js 公有方法通过 new Function 方式调用私有方法的问题? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ftfniqpl
V2EX    Node.js

js 公有方法通过 new Function 方式调用私有方法的问题?

  •  
  •   ftfniqpl 2016-05-22 17:46:25 +08:00 3866 次点击
    这是一个创建于 3457 天前的主题,其中的信息可能已经有所发展或是发生改变。
    var Circle = function() { var pi = function(){ return '3.14159'; }; this.area = function( str) { console.log(eval(str)()); //能正确调用 console.log(new Function('return '+str)); 如何调用?? }; } var c= new Circle(); c.area('pi'); 
    10 条回复    2016-05-23 09:27:05 +08:00
    littlepanzh
        1
    littlepanzh  
       2016-05-22 18:29:30 +08:00 via iPhone
    LZ 这个写法……可以当面试题了,考察变量作用域……典型的反面教材……
    SoloCompany
        2
    SoloCompany  
       2016-05-22 18:31:11 +08:00 via iPhone
    new function 和 eval 完全没区别,都是毫无安全性可言,你漏了执行 function 这一步而已
    ftfniqpl
        3
    ftfniqpl  
    OP
       2016-05-22 18:45:20 +08:00
    @SoloCompany 当我使用 new Function('return '+str).call());的时候,会提示 pi is not defined. 应该是 new Function 的时候需要使用 apply 传入作用域,不知道传啥?
    SoloCompany
        4
    SoloCompany  
       2016-05-22 18:54:39 +08:00 via iPhone
    @ftfniqpl 如果是这样的话就说明 new function 不能访问闭包了,建议去查一下 ecma 规范吧
    xavierchow
        5
    xavierchow  
       2016-05-22 22:46:58 +08:00
    Note: Functions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function constructor was called. This is different from using eval with code for a function expression.

    -- http://developer.mozilla.org/en-US/docs/Web/Javascript/Reference/Global_Objects/Function
    surgit
        6
    surgit  
       2016-05-22 23:06:26 +08:00
    不是 FUNCTION 的问题.
    c.area('pi'); 这里的 pi 肯定取不到你构造函数里的静态方法 pi 的.
    写代码还是少带点奇技淫巧的好.你这里传 function 作为参数.
    magicdawn
        7
    magicdawn  
       2016-05-23 08:07:20 +08:00
    new Function 只可访问全局变量哦~

    var Circle = function() {
    var pi = function(){
    return '3.14159';
    };

    var locals = {
    pi: pi
    };

    this.area = function( str) {
    return locals[str] && locals[str]();
    };
    }
    var c= new Circle();
    c.area('pi');
    magicdawn
        8
    magicdawn  
       2016-05-23 08:16:39 +08:00
    new Function 可以传参哦

    sagnitude
        9
    sagnitude  
       2016-05-23 08:30:19 +08:00
    应该是这儿
    http://www.ecma-international.org/ecma-262/5.1/index.html#sec-10.4.2

    eval: 10.4.2 的 2.b, 2.c , eval 会把当前执行 context 设为调用者的 context (VariableEnvironment)
    所以能调到局部变量

    new Function: 10.4.3 的 5 和 7 ,把当前执行环境设为 NewDeclarativeEnvironment(Function.prototype.[[Scope]]),所以得不到局部变量
    orcusfox
        10
    orcusfox  
       2016-05-23 09:27:05 +08:00
    就没有人给楼主一个正确的姿势吗。。
    http://jsbin.com/vefadiyidi/edit?js,console
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1273 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 17:21 PVG 01:21 LAX 09:21 JFK 12:21
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86