(Javascript)澄清数组文字中的“this”

时间:2022-11-27 17:09:37
var RPGConfig = (function() {
    var Constructor = function() {
        this.dir = "js";
        this.enginedir = "js/Engine";
    };
    Constructor.prototype = {
        include: [
            this.dir + "/Common.js",
            this.dir + "/Common/Class.js"
        ],
        test: function() {
            alert(this.dir);
        }
    };
    return Constructor;
})();
rpgConfig = new RPGConfig();
rpgConfig.test();
console.log(rpgConfig.include);

So, if I run rpgConfig.test(), the alert pops up with "js". Great! But, my rpgConfig.include shows up with "undefined" where this.dir should have printed "js" (as it did in test())...

所以,如果我运行rpgConfig.test(),警告会弹出“js”。大!但是,我的rpgConfig.include显示“undefined”,其中this.dir应该打印“js”(就像它在test()中所做的那样)...

So, how do I add "this" scope into an array literal?

那么,如何将“this”范围添加到数组文字中?

Thanks

谢谢

2 个解决方案

#1


3  

You simply can't, because prototype is made to "share" members between every instance of a class (and its descendency if never overridden). Which means you have to wrap it in a function for it to give you what you need.

你根本做不到,因为原型被用来在一个类的每个实例之间“共享”成员(如果从未被覆盖它的后代)。这意味着你必须将它包装在一个函数中,以便为你提供所需的东西。

Constructor.prototype = {
    include: function () {
        return [
            this.dir + "/Common.js",
            this.dir + "/Common/Class.js"
        ];
    },
    test: function() {
        alert(this.dir);
    }
};

#2


1  

The assignment to Constructor.prototype is evaluated first, before the constructor function. At the time it is evaluated, the constructor function has been declared, but not run, so the value of this.dir is undefined at that time.

首先在构造函数之前计算对Constructor.prototype的赋值。在评估时,构造函数已声明但未运行,因此此时未定义this.dir的值。

The reason the test() function works is because it grabs the value of this.dir on demand every time you call it, so by the time you call it, this.dir has already been assigned.

test()函数工作的原因是因为它每次调用时都会根据需要获取this.dir的值,所以当你调用它时,this.dir已经被分配了。

#1


3  

You simply can't, because prototype is made to "share" members between every instance of a class (and its descendency if never overridden). Which means you have to wrap it in a function for it to give you what you need.

你根本做不到,因为原型被用来在一个类的每个实例之间“共享”成员(如果从未被覆盖它的后代)。这意味着你必须将它包装在一个函数中,以便为你提供所需的东西。

Constructor.prototype = {
    include: function () {
        return [
            this.dir + "/Common.js",
            this.dir + "/Common/Class.js"
        ];
    },
    test: function() {
        alert(this.dir);
    }
};

#2


1  

The assignment to Constructor.prototype is evaluated first, before the constructor function. At the time it is evaluated, the constructor function has been declared, but not run, so the value of this.dir is undefined at that time.

首先在构造函数之前计算对Constructor.prototype的赋值。在评估时,构造函数已声明但未运行,因此此时未定义this.dir的值。

The reason the test() function works is because it grabs the value of this.dir on demand every time you call it, so by the time you call it, this.dir has already been assigned.

test()函数工作的原因是因为它每次调用时都会根据需要获取this.dir的值,所以当你调用它时,this.dir已经被分配了。