Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

javascript - 如何更新我的parsify()函数,使其可以与局部函数以及全局函数一起使用?(How to update my parsify() function such that it can go with local function along with global functions?)

I have two functions namely antiString() and parsify() whose code is given below :

(我有两个函数,分别是antiString()parsify(),其代码如下:)

 function antiString(obj) {
    return Function('"use strict";return (' + obj + ')')();
 };

 function parsify(str) { 
     return str.replace(/(
|
|
)/gm, " ")
           .replace(/@--funs(.*?)--@/g, function(_, mat) { 
              return antiString(mat); 
            }); 
 };

Let's us consider a function something like :

(让我们考虑一个类似以下的函数:)

 function ret(val) {
   return "I am " + val.toString()
 };

Now usage :

(现在用法:)

 var str = "Do you know who am I ? @--funs ret('Arcanadian') --@";
 console.log( parsify( str ) );
 // Output => Do you know whom am I ? I am Arcanadian

The code is beautiful but I have a problem with it !

(代码很漂亮,但是我有问题!)

This only works when the function inside the @--funs and --@ is global.

(仅当@-funs-@内部的函数为全局函数时,此方法才有效。)

Thus if I have a lot of functions than I have to declare a lot of global functions.

(因此,如果我有很多函数,则必须声明很多全局函数。)

So I think I should move to local one ( since local therfore not chance of interaction with global, and by the way these functions would be specially designed for the parsify() so I think local is better, but also I will not remove the support for global ones, since rewriting the same global function code in the local one doesn't make any sense, but yes their determination syntax will be different. @--lfuns & --@ will determine local functions while @--gfuns & --@ will determine global functions ) but I can't understand how to do so !

(因此,我认为我应该转向本地的(因为本地因此没有机会与全局交互,并且顺便说一下,这些函数将专门为parsify()设计,所以我认为本地更好,但也不会删除支持)对于全局函数,由于在本地代码中重写相同的全局函数代码没有任何意义,但是是的,它们的确定语法会有所不同。 @-lfuns-@将确定本地函数,而@-gfuns- -@将确定全局函数),但我不知道该怎么做!)

But I can assume the new us of the parsify() function would be something like this :

(但是我可以假设parsify()函数的新功能是这样的:)

 function q(val) {
   return "So " + val.toString();
 };

 var str = "@--lfuns name('Arc') --@. So @--gfuns q('can you help ?') --@";
 console.log( 
    parsify( str, {
      name: function(val) {
        return "I am " + val.toString();
      }
    })
 ); 

 // Output => I am Arc. So can you help ?

So, can you teach you where to update my code ?

(因此,您可以教您在哪里更新我的代码吗?)

Thanks in advance

(提前致谢)

  ask by Arcanadian Arc translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...