JS - 通过原型填充数组

JS - fill an array via prototyping

本文关键字:填充 数组 原型 JS      更新时间:2023-09-26

我刚刚阅读了以下JS代码,我得到了2个问题:

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

爪哇语

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

Apply 在推送函数中设置调用对象。但它似乎在这里没有正确使用。有关调用对象、构造函数和原型的更多信息,请参见此处:https://stackoverflow.com/a/16063711/1641941