2015-01-20 72 views
0

填充数组我刚才读下面的JS代码,我得到了2个问题:JS - 通过原型

  1. 为什么没有容器对象声明为变量?
  2. 什么是“Array.prototype.push.apply”?

的Javascript

Container = function(title, used) { 
    this.title = title; 
    this.used= !!used; 
    this.callbacks = []; 
}; 

Container.prototype.push = function() { 
    Array.prototype.push.apply(this.callbacks); 
}; 
+0

1)因为这是一个错误。或者已经在其他地方宣布了 – Bergi 2015-01-20 23:27:04

+1

2)看看[应用程序的功能](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply)。但是,您的Containers'push'方法没有太多意义,它等同于'this.callbacks.push()' - 没有任何参数。 – Bergi 2015-01-20 23:28:26

+0

应该是.apply(this.callbacks,arguments); – dandavis 2015-01-20 23:30:31

回答