gpt4 book ai didi

javascript - JS 的 for-in 和 in

转载 作者:行者123 更新时间:2023-12-03 20:56:51 27 4
gpt4 key购买 nike

var result = function(){}
console.log("constructor" in result);//true
console.log("__proto__" in result);//true
console.log("prototype" in result);//true

for (var prop in result) {
console.log(prop); // no output!!
}

当我使用 in 来确定结果中是否有属性时,它返回 true;但是当我使用for-in时,结果中没有属性,为什么?

最佳答案

引用:https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in

A for...in loop does not iterate over non–enumerable properties. Objects created from built–in constructors like Array and Object have inherited non–enumerable properties from Object.prototype and String.prototype that are not enumerable, such as String's indexOf method or Object's toString method. The loop will iterate over all enumerable properties of the object or that it inherits from its constructor's proptotype (including any which overwrite built-in properties).

关于javascript - JS 的 for-in 和 in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9530471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com