【问题标题】:Javascript Reference Outer Object From Inner ObjectJavascript 从内部对象引用外部对象
【发布时间】:2010-05-31 00:48:39
【问题描述】:

好的,我看到了一些针对 Java 的参考,但不是 javascript(希望您知道它们完全不同)。所以这里是特定的代码:

function Sandbox() {
    var args = Array.prototype.slice.call(arguments)
        , callback = args.pop()
        , modules = (args[0] && typeof args[0] === 'string' ? args : args[0])
        , i;

    if (!(this instanceof Sandbox)) {
        return new Sandbox(modules, callback);
    }

    if (!modules || modules[0] === '*') {
        modules = [];
        for (i in Sandbox.modules) {
            if (Sandbox.modules.hasOwnProperty(i)) {
                modules.push(i);
            }
        }
    }

    for (i = 0; i < modules.length; i++) {
        Sandbox.modules[modules[i]](this);
    }

    this.core = {
        'exp': {
            'classParser': function (name) {
                return (new RegExp("(^| )" + name + "( |$)"));
            },
            'getParser': /^(#|\.)?([\w\-]+)$/
        },
        'typeOf': typeOf,
        'hasOwnProperty': function (obj, prop) {
            return obj.hasOwnProperty(prop);
        },
        'forEach': function (arr, fn, scope) {
            scope = scope || config.win;
            for (var i = 0, j = arr.length; i < j; i++) {
                fn.call(scope, arr[i], i, arr);
            }
        }
    };

    this.config = {
        'win' : win,
        'doc' : doc
    };

    callback(this);
}

如何从 this.core.forEach 中访问 this.config.win?或者这不可能?

【问题讨论】:

    标签: javascript object reference


    【解决方案1】:

    在 Sandbox() 函数体中,像这样:

    var self = this;
    

    然后;

    self.config.win
    

    上下文相关的“this”发生变化时的核心部分

    【讨论】:

    • 吹毛求疵,但这不是闭包。
    • 非常感谢,除了沙盒功能外,我在所有地方都试过了(长时间的工作可能没有帮助)。
    • 我看到that 使用了很多。 var that = this;.
    • 你是对的。我最初是在写其他东西('var Sandbox = function (){...return function() {} }()')但是,意识到这没有必要。感谢您的帮助
    【解决方案2】:

    您也可以使用函数表达式,但这要求您在 this.core 之前定义 this.config。这可以防止您必须使用局部变量。但是请注意,这会捕获该值而不是引用它,因此如果您为config 分配一个新值,它将对forEach 没有影响。但是,您仍然可以更改 config.win,因为该对象未被克隆。这种行为可能很微妙,如果使用不当会导致错误。

    说了这么多,最好按照乔纳森的建议使用var self = this;。然而,这是另一种方法,并且在某些情况下很有用。

    this.config = {
        'win' : "win",
        'doc' : "doc"
    };
    
    this.core = {
        'exp': {
            'classParser': function (name) {
                return (new RegExp("(^| )" + name + "( |$)"));
            },
            'getParser': /^(#|\.)?([\w\-]+)$/
        },
        'typeOf': typeOf,
        'hasOwnProperty': function (obj, prop) {
            return obj.hasOwnProperty(prop);
        },
        'forEach': (function(config){
            function (arr, fn, scope) {
                scope = scope || config.win;
                for (var i = 0, j = arr.length; i < j; i++) {
                    fn.call(scope, arr[i], i, arr);
                }
            }
        })(this.config)
    }
    

    【讨论】:

    • 虽然它对我正在做的事情没有用,但由于我很欣赏处理它的替代方法(永远不知道它可能会派上用场),因此得到了评价。非常感谢先生的回答。
    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    相关资源
    最近更新 更多