Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.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 - Fatal编程技术网

如何在javascript中访问子类内部父类的方法

如何在javascript中访问子类内部父类的方法,javascript,Javascript,我有一个“model”类/原型定义如下,它有一个名为“given”的子类,该子类尝试访问model类的方法'getNodes()” 但它对“this.getNodes”给出了一个例外,表示未定义 var model = { constructor: function(/*string*/ mode, /*string*/ name, /*object*/ properties) { this._mode = mode; this.

我有一个“
model
”类/原型定义如下,它有一个名为“
given
”的子类,该子类尝试访问model类的方法
'getNodes()

但它对“
this.getNodes
”给出了一个例外,表示未定义

 var model = {
        constructor: function(/*string*/ mode, /*string*/ name, /*object*/ properties) {
            this._mode = mode;
            this.beginX = 100;
            this.beginY = 100;
            this.nodeWidth = 200;
            this.nodeHeight = 200;
            this.x = this.beginX;
            this.y = this.beginY;
            this.lastNodeVisible = null;
            this.ID = 1;
            this.taskName = name;
            this.properties = properties;
            this.checkedNodes = new Array();
      //      this.model = #call_build_method;

            /* 
             add subclasses with model accessors 
             */
            this.given = {
            getNodes: this.getNodes,
            setNodeName: this.setNodeName
        };
      },
      getNodes: function() {
            // Summary: returns an array containing the nodes in the given model 
            return #someobject;
        },
}

您需要将此变量存储在outter作用域中:

this.model = <SOMETHING>;
var self = this;
this.given = {
    getNodes: function(){self.getNodes(self.model);}
    // inside a function this is this.given
};
this.model=;
var self=这个;
这是给定的={
getNodes:function(){self.getNodes(self.model);}
//在函数内部,这是给定的
};

我假设您希望调用父类中具有正确作用域的方法。 以下是两种方法,一种使用dojo挂接装置,另一种不使用:

require([
    "dojo/_base/lang"
],function(lang){
    model = function(){
        var obj = {
            data: "ok",
            getData4: function(){
                return this.data;
            }  
        };

        obj.sub = {
            getData5: lang.hitch(obj, obj.getData4),
            getData6: function(){return obj.getData4.apply(obj,arguments);}
        };

        return obj;
    };

    m = new model();
    console.log("call getData4: ", m.getData4());  // returns "ok"
    console.log("call getData5: ", m.sub.getData5());  // returns "ok"
    console.log("call getData6: ", m.sub.getData6());  // returns "ok"
});

当你有一个关于
的函数时,要三思而后行
这取决于调用该函数的方式。在您的情况下,您需要将其缓存在更高的范围内,如
var self=this
。请看任何代码示例如何调用该构造函数?它与dojo有什么关系?无论如何,在javascript中对this这个词有一个基本的了解:您可能会发现这很有帮助。