小编lil*_*tle的帖子

注入函数中的Javascript范围

(function(){
    var privateSomething = "Boom!";
    var fn = function(){}
    fn.addFunc = function(obj) {
        alert('Yeah i can do this: '+privateSomething);
        for(var i in obj) fn[i] = obj[i];
    }
    window.fn=fn;
})();

fn.addFunc({
    whereAmI:function()
    {
        alert('Nope I\'ll get an error here: '+privateSomething);
    }
});

fn.whereAmI();
Run Code Online (Sandbox Code Playgroud)

为什么whereAmI()无法访问privateSomething?以及如何将whereAmI()放在与addFunc()相同的上下文中?

javascript scope

6
推荐指数
1
解决办法
155
查看次数

标签 统计

javascript ×1

scope ×1