Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在命名空间函数中引用所需对象_Javascript_Namespaces_This - Fatal编程技术网

Javascript 在命名空间函数中引用所需对象

Javascript 在命名空间函数中引用所需对象,javascript,namespaces,this,Javascript,Namespaces,This,我在引用所需对象时遇到问题,因为我使用了命名空间函数 这里没有问题: obj.test = function() { // this == obj } 但当我说: obj.namespace.test = function() { // this == namespace } 在后一个例子中,我知道这个引用了名称空间,但我想引用obj。我该怎么做呢?没有简单的答案,但您有几个选择: obj.namespace.test = function () { return (functi

我在引用所需对象时遇到问题,因为我使用了命名空间函数

这里没有问题:

obj.test = function() {
  // this == obj
}
但当我说:

obj.namespace.test = function() {
  // this == namespace
}

在后一个例子中,我知道
这个
引用了
名称空间
,但我想引用
obj
。我该怎么做呢?

没有简单的答案,但您有几个选择:

obj.namespace.test = function () {
  return (function () {
     // this == obj
  }).apply(obj, Array.prototype.slice.call(arguments));
};
这将返回一个绑定到
obj
的函数。不幸的是,如果重新分配了
obj
,这将不起作用,因为它是一个实时引用。这一点更为可靠:

obj.namespace.test = (function (obj) {
   return function () {
     return (function () {
        // this == obj
     }).apply(obj, Array.prototype.slice.call(arguments));
   };
}(obj));

正如你所看到的,这两个都不是很干净。你可能会问自己,为什么要从
这个
开始。显然,使用对obj的正常引用是最直接的方法。

其他人提出了一些好的建议

我只是想补充一点,您还可以将namespace设置为一个函数,该函数将返回一个对象,其中包含一个指向obj的变量以及您想要的任何成员函数

例如:

    // Note that "namespace" is a reserved word in JS for some reason,
    //  so you can't use it as a variable/function/etc name.
    var myNamespace = function myNamespace(){
        var that = this;

        var test = function test(){
            //in here use this.that to point to obj
            alert(this.that.name);
        };

        return {that: that, test: test};
    };

    // Then create the obj:
    var obj = { name: "Mr. Ahb Jeckt", myNamespace: myNamespace};

    // Then you can just call the "namespace" and member function like this:
    obj.myNamespace().test();


    //Or, "initialize" the namespace and call it like so:
    obj.myNamespace = obj.myNamespace();
    obj.myNamespace.test();

    obj.name = "Mrs Ahb Jeckt";
    obj.myNamespace.test();
这样,在“名称空间”本身中就没有对obj的硬编码引用,而且我认为它非常干净

如果obj是一个“类”,这也适用;只需将obj设置为构造函数而不是对象文字:

// Then create the obj:
var obj = function (name){
    this.name = name || "unnamed";
    this.myNamespace = myNamespace;

    // Initialize the namespace, we can leave this out and just reference
    //    obj.myNamespace() each time as well

    this.myNamespace = this.myNamespace();
};

// Then you can just call the "namespace" and member function like this:

var myObj = new obj("Mr Ahb Jeckt");
myObj.myNamespace.test();

var myObj2 = new obj("Mrs Ahb Jeckt");
myObj2.myNamespace.test();