2010-02-18 42 views
5

在Javascript中,有没有一种方法可以从一个字符串(例如通过新的Function()构造函数)创建一个函数,并让它继承父范围?例如:从继承父范围的字符串创建函数

(function(){ 
    function yay(){ 
    } 
    var blah = "super yay" 
    yay.prototype.testy = new Function("alert(blah)") 
    yay.prototype.hello = function(){alert(blah)} 
    whee = new yay(); 
    whee.hello() 
    whee.testy() 
})() 

有没有什么办法让whee.testy()也提醒“超级耶”?

+2

这是否有一些原因downvoted? – cletus 2010-02-18 03:00:39

+1

我不认为这是可能的。 – SLaks 2010-02-18 03:04:48

+1

我认为你不应该这样做。真的,这可能是非常危险的。从字符串创建函数的目的是什么?你想达到什么目的? – 2010-02-18 15:06:20

回答

1
(function(){ 
    function yay(){ 
    } 
    var blah = "super yay" 
    yay.prototype.testy = eval("(function(){alert(blah)})")//new Function("alert(blah)") 
    yay.prototype.hello = function(){alert(blah)} 
    whee = new yay(); 
    whee.hello() 
    whee.testy() 
})() 

这似乎为我工作,并没有eval'd数据是来自不受信任的来源。它只是用于缩小代码。

-1

您的意思是eval?

(function(){ 
    function yay(){ 
    } 
    var blah = "super yay" 
    yay.prototype.testy = new Function(eval("alert(blah)")) // <--------------- 
    yay.prototype.hello = function(){alert(blah)} 
    whee = new yay(); 
    whee.hello() 
    whee.testy() 
})() 

然而,JS在这里有两个道德上令人反感的特征。 1)eval是“evil”,并且2)eval中的代码可以看到外部的变量。

+0

'eval'是**恶**!不要使用它!别! – 2010-02-18 03:23:54

+0

这里有一个更好的解释,为什么不使用eval:http://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil/198031#198031 – 2010-02-18 03:31:12

+0

就像我说的:morally令人反感 – 2010-02-18 04:25:35

1

事实上,结合functioneval应该做你想要什么:

// blah exists inside the 'hello' function 
yay.prototype.hello = function(){ alert(blah) } 
// blah also exists inside the 'testy' function, and 
// is therefore accessible to eval(). 
yay.prototype.testy = function(){ eval('alert(blah)') } 
+0

但是OP必须非常确定该字符串不包含任何恶意代码。我对字符串赋值的方式很感兴趣 – 2010-02-18 13:24:51

+10

是的,是的...... eval是邪恶的,所有这一切都是通过OP创建匿名的方式来判断的围绕他的代码运行,这不是他第一天做JS的时间。 – levik 2010-02-18 20:58:17

+0

我只是使用eval/Function来更好地压缩一些代码,它不是用户输入的东西,但我只是有一些代码,其中有很多函数,只有很少的代码,function(e){var t = this; return e(t)?[t]:[]}并且有大量的字节被函数(e){var t = this; return}用完。 – antimatter15 2010-02-19 21:02:15

-1

你不需要评估。

您必须将blah连接到字符串函数,但JavaScript会抱怨没有“;”就在连接之前,这是因为当连接它时,blah只是一些文本。你必须逃脱两个“\””周围的变量等等,以便使它看起来像在它的文本字符串。

(function(){ 
    function yay(){ 
    } 
var blah = "super yay" 

yay.prototype.testy = new Function("alert(\""+blah+"\")") 
yay.prototype.hello = function(){alert(blah)} 
whee = new yay(); 
whee.hello() 
whee.testy() 
})() 

这将提醒‘超级耶’的两倍!

+0

但是如果我更新了“blah”的值,那么whee.testy()会提醒错误的值。 – antimatter15 2010-03-05 20:29:40

0

原因为什么whee.testy不工作是因为使用声明一个new Function("alert(blah)")功能创建当前封闭外的函数。由于blah是你的闭包内定义的,你没有访问它,它抛出一个未定义的错误。

这里是概念验证示例:

var blah = "global (window) scope"; 

(function(){ 
    function yay(){ 
    } 
    var blah = "closure scope"; 

    yay.prototype.testy = new Function("alert(blah)"); 
    yay.prototype.hello = function(){ alert(blah); } 
    whee = new yay(); 
    whee.hello(); 
    whee.testy(); 
})(); 
+0

我知道它为什么不起作用,但有没有办法让它工作? – antimatter15 2010-03-05 20:30:01

+0

@ antimatter15:No ...'new Function(string)'在全局范围内定义一个函数。这是设计。改用匿名函数。 – 2010-03-05 22:08:43

1

这应该给你想要的东西......

var inputAction = "alert(blah)"; 
yay.prototype.testy = eval("(function(){ " + inputAction + "; })")

它基本上包装了一个匿名函数内部预期的作用,这得到充分的评估EVAL,但不运行的时候了,它被包装成一个函数。

你可以在这里走得更远,但不知道你想完成什么,这很难说。