Javascript 在其自身原型(js)的数组中调用函数时出错

Javascript 在其自身原型(js)的数组中调用函数时出错,javascript,arrays,prototype,Javascript,Arrays,Prototype,抱歉,如果这是一个愚蠢的问题,我只是在试验,不明白为什么这不起作用: var MyFunc = function() {}; MyFunc.prototype = { receive: function() {

抱歉,如果这是一个愚蠢的问题,我只是在试验,不明白为什么这不起作用:

var MyFunc = function() {};                                                    

MyFunc.prototype = {                                                           
  receive: function() {                                                        
    console.log("Received");                                                   
  },                                                                           
  spine: [MyFunc],                                                             
}                                                                              

var func = new MyFunc();                                                       

func.receive();          //works                                               
func.spine[0].receive(); //error: Object function () {} has no method 'receive'
最后一行是错误行

全输出:

Received

/home/zxcv/Documents/CODE/js/stuff/gp/scratch.js:13
func.spine[0].receive(); //error: Object function () {} has no method 'receive
              ^
TypeError: Object function () {} has no method 'receive'
    at Object.<anonymous> (/home/USER/Documents/CODE/js/stuff/gp/scratch.js:13:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

shell returned 8
已收到
/home/zxcv/Documents/CODE/js/stuff/gp/scratch.js:13
函数脊椎[0]。接收()//错误:对象函数(){}没有方法“receive”
^
TypeError:对象函数(){}没有方法“receive”
反对。(/home/USER/Documents/CODE/js/stuff/gp/scratch.js:13:15)
在模块处编译(Module.js:456:26)
在Object.Module.\u extensions..js(Module.js:474:10)
在Module.load(Module.js:356:32)
在Function.Module.\u加载(Module.js:312:12)
位于Function.Module.runMain(Module.js:497:10)
启动时(node.js:119:16)
在node.js:929:3
壳牌返回8号

因为
spine[0]
是MyFunc(构造函数函数对象),而不是MyFunc的实例对象。
prototype
中定义的函数放在构造函数创建的实例上,而不是放在构造函数本身上

为了执行该函数,您必须遍历原型

func.spine[0].prototype.receive();
如果您想要创建一个可以直接从MyFunc执行的方法,那么您需要在MyFunc上实际定义它

MyFunc.receive2 = function(){
    console.log("Received");
};
var func = new MyFunc();
func.spine[0].receive2();
//Or
MyFunc.receive2();

//Note you would not be able to call this directly from the instance
func.receive2(); //would cause an error

因为
spine[0]
是MyFunc(构造函数函数对象),而不是MyFunc的实例对象。
prototype
中定义的函数放在构造函数创建的实例上,而不是放在构造函数本身上

为了执行该函数,您必须遍历原型

func.spine[0].prototype.receive();
如果您想要创建一个可以直接从MyFunc执行的方法,那么您需要在MyFunc上实际定义它

MyFunc.receive2 = function(){
    console.log("Received");
};
var func = new MyFunc();
func.spine[0].receive2();
//Or
MyFunc.receive2();

//Note you would not be able to call this directly from the instance
func.receive2(); //would cause an error

因为
spine[0]
是MyFunc(构造函数函数对象),而不是MyFunc的实例对象。
prototype
中定义的函数放在构造函数创建的实例上,而不是放在构造函数本身上

为了执行该函数,您必须遍历原型

func.spine[0].prototype.receive();
如果您想要创建一个可以直接从MyFunc执行的方法,那么您需要在MyFunc上实际定义它

MyFunc.receive2 = function(){
    console.log("Received");
};
var func = new MyFunc();
func.spine[0].receive2();
//Or
MyFunc.receive2();

//Note you would not be able to call this directly from the instance
func.receive2(); //would cause an error

因为
spine[0]
是MyFunc(构造函数函数对象),而不是MyFunc的实例对象。
prototype
中定义的函数放在构造函数创建的实例上,而不是放在构造函数本身上

为了执行该函数,您必须遍历原型

func.spine[0].prototype.receive();
如果您想要创建一个可以直接从MyFunc执行的方法,那么您需要在MyFunc上实际定义它

MyFunc.receive2 = function(){
    console.log("Received");
};
var func = new MyFunc();
func.spine[0].receive2();
//Or
MyFunc.receive2();

//Note you would not be able to call this directly from the instance
func.receive2(); //would cause an error

因为在赋值时myFunc仍然只是一个没有继承方法的函数。因为在赋值时myFunc仍然只是一个没有继承方法的函数。因为在赋值时myFunc仍然只是一个没有继承方法的函数。因为在赋值时myFunc仍然只是一个没有继承方法的函数继承的方法。