Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么Object.keys(Array.prototype)返回空数组?_Javascript_Arrays - Fatal编程技术网

Javascript 为什么Object.keys(Array.prototype)返回空数组?

Javascript 为什么Object.keys(Array.prototype)返回空数组?,javascript,arrays,Javascript,Arrays,首先遵循此代码: function User(name, dept){ this.username = name; this.dept = dept; this.talk = function(){ return "Hi"; }; } function Employee(){ User.apply(this, Array.prototype.slice.call(arguments)); } Employee.prototype = n

首先遵循此代码:

function User(name, dept){
    this.username = name;
    this.dept = dept;
    this.talk = function(){
        return "Hi";
    };
}

function Employee(){
    User.apply(this, Array.prototype.slice.call(arguments));
}

Employee.prototype = new User();


for(var x in Employee.prototype) 
    console.log(x, ':', Employee.prototype[x]);

Object.keys(Employee.prototype);//prints array of keys...
打印出来的很好

Array.prototype; //[]
Array.prototype.slice; // Function

var o = Array.prototype;

for(var i in o) console.log(i, ':', o[i]); //it doesn't execute
Object.keys(Array.prototype); //[] - ???
如何解释这种行为

我们可以为我们尝试创建的构造函数模仿这个吗?

Object.keys()-

该方法返回给定对象自身的可枚举属性的数组

我们可以检查给定属性是否可枚举:

Array.prototype.propertyIsEnumerable('push');
// false
或者,我们可以获得对象属性的完整描述符,其中还包括可枚举性标志

Object.getOwnPropertyDescriptor(Array.prototype, 'push');
// {writeable: true, enumerable: false, configurable: true}

上的属性故意是不可枚举的,这样它们就不会出现在中。

因为它们是不可枚举的属性。Object.getOwnPropertyNames(Array.prototype)大约是有人回答这个问题的时候了…:)