我可以将 javascript Prototype 添加到我的数组实例吗?
Can I add javascript Prototype to my array instance?
所以我了解到您可以向 javascript 对象原型添加新方法。
myObject.prototype.myFunction = function {}
。我想对我的数组实例做同样的事情,但不断出错。像这样的东西。
var myArray = [1,2,3,4,5,6,7,8,9,0];
myArray.prototype.bubbleSort = function(){
// sort stuff here.
}
为什么不允许这样做?我认为既然数组继承自对象,你应该能够像对象一样扩展它们。
如果这不可能,我该如何添加一个 .符号函数调用我的数组,所以我可以说。 myArray.bubbleSort();
而不是 bubbleSort(myArray);
,而不向 Array.prototype 添加方法。
我想我的问题引起了一些混乱,所以请在回答之前阅读这一部分。谢谢
我的目标是拥有一个名为 myArray
的数组,该数组具有特定的 bubbleSort 方法。我不想改变主要 Array.Prototype。
所以以后我可以做这样的事情。 var yourArray = new myArray()
所以现在因为 yourArray
是 myArray
的实例,所以它可以访问 bubblesort 函数。
我们可以在不改变主Array.prototype的情况下实现这个吗?
你应该用过Array.prototype.bubbleSort
var myArray = [1,2,3,4,5,6,7,8,9,0];
Array.prototype.bubbleSort = function(){
console.log("Do bubble sort here");
}
myArray.bubbleSort();
数组也是对象,因此您可以轻松地将其作为函数添加到该数组:
var myArray = [1,2,3,4,5,6,7,8,9,0];
myArray.bubbleSort = function(){console.log(this)}
请注意,如果您在库中执行此操作,您可能会像这样破坏代码:
for(var i in myArray){
console.log(i)
}
因为这也会给出 bubbleSort 属性
更好的方法是扩展数组以制作包装器:
function sortableArray(){
return this
}
sortableArray.prototype = Object.create(Array.prototype);
sortableArray.prototype.bubbleSort = function(){
console.log(this)
}
var arr = new sortableArray()
arr.push(1)
arr.push(0)
arr.push(9)
arr.bubbleSort();
我认为您误解了原型的用途。原型为给定对象的 link 其他属性和方法提供了对象链。
例如,当你创建一个数组时
[]
该数组可以访问 forEach
、reduce
等各种函数,因为它们是 linked 到原型或通过原型继承的。修改该原型将修改从同一原型继承的所有对象。
var aNewArray = [6, 7, 8];
var myArray = [1, 2, 3, 4, 5];
// myArray.constructor.prototype will give you access to myArrays prototype
myArray.constructor.prototype.forMyArrayOnly = function() {
console.log(this === myArray ? 'this is myArray' : 'this is NOT myArray!');
};
myArray.forMyArrayOnly();
aNewArray.forMyArrayOnly();
如果您只想添加一个方法或属性到一个单个对象,您不想修改它们的原型链。相反,您想将 属性 直接添加到对象中。
var myArray = [1, 2, 3, 4, 5];
myArray.bubbleSort = function() {
console.log('I totally just did bubble sort. Check it out');
console.log(this);
};
myArray.bubbleSort();
所以我了解到您可以向 javascript 对象原型添加新方法。
myObject.prototype.myFunction = function {}
。我想对我的数组实例做同样的事情,但不断出错。像这样的东西。
var myArray = [1,2,3,4,5,6,7,8,9,0];
myArray.prototype.bubbleSort = function(){
// sort stuff here.
}
为什么不允许这样做?我认为既然数组继承自对象,你应该能够像对象一样扩展它们。
如果这不可能,我该如何添加一个 .符号函数调用我的数组,所以我可以说。 myArray.bubbleSort();
而不是 bubbleSort(myArray);
,而不向 Array.prototype 添加方法。
我想我的问题引起了一些混乱,所以请在回答之前阅读这一部分。谢谢
我的目标是拥有一个名为 myArray
的数组,该数组具有特定的 bubbleSort 方法。我不想改变主要 Array.Prototype。
所以以后我可以做这样的事情。 var yourArray = new myArray()
所以现在因为 yourArray
是 myArray
的实例,所以它可以访问 bubblesort 函数。
我们可以在不改变主Array.prototype的情况下实现这个吗?
你应该用过Array.prototype.bubbleSort
var myArray = [1,2,3,4,5,6,7,8,9,0];
Array.prototype.bubbleSort = function(){
console.log("Do bubble sort here");
}
myArray.bubbleSort();
数组也是对象,因此您可以轻松地将其作为函数添加到该数组:
var myArray = [1,2,3,4,5,6,7,8,9,0];
myArray.bubbleSort = function(){console.log(this)}
请注意,如果您在库中执行此操作,您可能会像这样破坏代码:
for(var i in myArray){
console.log(i)
}
因为这也会给出 bubbleSort 属性
更好的方法是扩展数组以制作包装器:
function sortableArray(){
return this
}
sortableArray.prototype = Object.create(Array.prototype);
sortableArray.prototype.bubbleSort = function(){
console.log(this)
}
var arr = new sortableArray()
arr.push(1)
arr.push(0)
arr.push(9)
arr.bubbleSort();
我认为您误解了原型的用途。原型为给定对象的 link 其他属性和方法提供了对象链。
例如,当你创建一个数组时
[]
该数组可以访问 forEach
、reduce
等各种函数,因为它们是 linked 到原型或通过原型继承的。修改该原型将修改从同一原型继承的所有对象。
var aNewArray = [6, 7, 8];
var myArray = [1, 2, 3, 4, 5];
// myArray.constructor.prototype will give you access to myArrays prototype
myArray.constructor.prototype.forMyArrayOnly = function() {
console.log(this === myArray ? 'this is myArray' : 'this is NOT myArray!');
};
myArray.forMyArrayOnly();
aNewArray.forMyArrayOnly();
如果您只想添加一个方法或属性到一个单个对象,您不想修改它们的原型链。相反,您想将 属性 直接添加到对象中。
var myArray = [1, 2, 3, 4, 5];
myArray.bubbleSort = function() {
console.log('I totally just did bubble sort. Check it out');
console.log(this);
};
myArray.bubbleSort();