difference between Function and new Function

前端 未结 4 1183
萌比男神i
萌比男神i 2020-12-11 03:37

I sometimes see people doing this Function(\'alert(\"hi\")\') and sometimes they do new Function(\'alert(\"hi\")\')

Is there a difference b

4条回答
  •  情书的邮戳
    2020-12-11 04:23

    There is only one usage of new Function. It is this:

    var constructor = new Function;
    
    // then this 
    
    constructor.prototype = { 
        //...  stuff
    };
    
    // OR
    
    constructor.prototype = new someOtherThing;
    
    // then
    
    var obj = new constructor;
    

    Basically you can use new Function if you want to create an object constructor with an empty constructor that only uses inheritance.

    If you want a function to do anything then don't use Function at all.

提交回复
热议问题