默认情况下,数组没有原型吗?

时间:2022-09-25 14:08:00

I was hoping to be able to augment Array.prototype with methods and then call them on any array:

我希望能够使用方法扩充Array.prototype,然后在任何数组上调用它们:

>>> [1, 2, 3].customMethod();

But it appears arrays have no prototype...?

但似乎阵列没有原型......?

>>> [1, 2, 3].prototype
undefined

Am I missing something here?

我在这里错过了什么吗?


It appears my actual problem lies elsewhere: calling [1, 2, 3].customMethod() works, but calling someDomElement.childNodes.customMethod() fails. Is childNodes not a real array?

看来我的实际问题在于其他地方:调用[1,2,3] .customMethod()可以工作,但是调用someDomElement.childNodes.customMethod()会失败。 childNodes不是真正的数组吗?

childNodes.filter is not a function

2 个解决方案

#1


3  

prototype is a property of constructor functions, like Array. So Array.prototype exists, but not [1, 2, 3].prototype; Array is a constructor function, while [1, 2, 3] is an array.

prototype是构造函数的属性,如Array。所以Array.prototype存在,但不存在[1,2,3] .prototype; Array是构造函数,而[1,2,3]是一个数组。

You are looking for Object.getPrototypeOf([1, 2, 3]).

您正在寻找Object.getPrototypeOf([1,2,3])。

Object.getPrototypeOf is an ECMAScript 5 method, and as such may not be present in all browsers. In which case, you can try accessing the __proto__ property, i.e. [1, 2, 3].__proto__, which is an older, nonstandard thing that Object.getPrototypeOf is the new standard version of, or you can use an ES5 shim to ensure that wherever __proto__ is supported, so is Object.getPrototypeOf.

Object.getPrototypeOf是ECMAScript 5方法,因此可能并非在所有浏览器中都存在。在这种情况下,你可以尝试访问__proto__属性,即[1,2,3] .__ proto__,这是一个较旧的非标准的东西,Object.getPrototypeOf是新的标准版本,或者你可以使用ES5垫片来确保在支持__proto__的地方,Object.getPrototypeOf也是如此。

#2


2  

It looks like you're working with a DOM NodeList, which is not the same thing as a JavaScript array object.

看起来你正在使用DOM NodeList,它与JavaScript数组对象不同。

http://blog.duruk.net/2011/06/19/nodelists-and-arrays-in-javascript/ should provide some insight.

http://blog.duruk.net/2011/06/19/nodelists-and-arrays-in-javascript/应提供一些见解。

To obtain a 'real' javascript array from an Array-like object (such as a NodeList or the arguments variable), use the .slice method, like so:

要从类似Array的对象(例如NodeList或arguments变量)获取“真正的”javascript数组,请使用.slice方法,如下所示:

var realArray = Array.prototype.slice.call(someDomElement.childNodes);
realArray.filter()

And yes, like another answer indicated - the .prototype object is only a property of the constructor function - not of instances. eg. Object.prototype exists, but ({}).prototype is undefined.

是的,就像另一个答案所示 - .prototype对象只是构造函数的一个属性 - 而不是实例。例如。 Object.prototype存在,但是({})。原型未定义。

#1


3  

prototype is a property of constructor functions, like Array. So Array.prototype exists, but not [1, 2, 3].prototype; Array is a constructor function, while [1, 2, 3] is an array.

prototype是构造函数的属性,如Array。所以Array.prototype存在,但不存在[1,2,3] .prototype; Array是构造函数,而[1,2,3]是一个数组。

You are looking for Object.getPrototypeOf([1, 2, 3]).

您正在寻找Object.getPrototypeOf([1,2,3])。

Object.getPrototypeOf is an ECMAScript 5 method, and as such may not be present in all browsers. In which case, you can try accessing the __proto__ property, i.e. [1, 2, 3].__proto__, which is an older, nonstandard thing that Object.getPrototypeOf is the new standard version of, or you can use an ES5 shim to ensure that wherever __proto__ is supported, so is Object.getPrototypeOf.

Object.getPrototypeOf是ECMAScript 5方法,因此可能并非在所有浏览器中都存在。在这种情况下,你可以尝试访问__proto__属性,即[1,2,3] .__ proto__,这是一个较旧的非标准的东西,Object.getPrototypeOf是新的标准版本,或者你可以使用ES5垫片来确保在支持__proto__的地方,Object.getPrototypeOf也是如此。

#2


2  

It looks like you're working with a DOM NodeList, which is not the same thing as a JavaScript array object.

看起来你正在使用DOM NodeList,它与JavaScript数组对象不同。

http://blog.duruk.net/2011/06/19/nodelists-and-arrays-in-javascript/ should provide some insight.

http://blog.duruk.net/2011/06/19/nodelists-and-arrays-in-javascript/应提供一些见解。

To obtain a 'real' javascript array from an Array-like object (such as a NodeList or the arguments variable), use the .slice method, like so:

要从类似Array的对象(例如NodeList或arguments变量)获取“真正的”javascript数组,请使用.slice方法,如下所示:

var realArray = Array.prototype.slice.call(someDomElement.childNodes);
realArray.filter()

And yes, like another answer indicated - the .prototype object is only a property of the constructor function - not of instances. eg. Object.prototype exists, but ({}).prototype is undefined.

是的,就像另一个答案所示 - .prototype对象只是构造函数的一个属性 - 而不是实例。例如。 Object.prototype存在,但是({})。原型未定义。