Reflection in JavaScript array

how to execute a loop through the functions of a member of a JavaScript array, the following code does not work: (

for (var i in Array.prototype){
    alert(i)
} //show nothing 

for (var i in []){
   alert(i)
} // show nothing
+3
source share
2 answers

We don’t list any of the initial properties of the prototype, but you can find out exactly what you are looking for in the ECMA specification:

http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf

You can only list the properties that you defined, for example:

Object.prototype.foo = function(){};

x = {};

for ( var prop in x ) {
    alert( prop );
}

will warn:

Foo

: object.hasOwnProperty( property ) a for..in loop , , , Object.prototype.

+7

.

+3

Source: https://habr.com/ru/post/1719343/


All Articles