1 回答

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊
in不檢查自身的屬性class。它檢查類(lèi)實(shí)例中的屬性。
function Shape() {
aaa = 10
}
Shape.prototype.bbb = 20
function Square() {}
Square.prototype = Object.create(Shape.prototype)
Square.prototype.constructor = Square;
const instance = new Square();
console.log('aaa' in instance) // false
console.log('bbb' in instance) // true
在這里,當(dāng)您將屬性分配給某些原型時(shí),function并不意味著該函數(shù)將具有這些屬性。原型上的這些屬性將添加到對(duì)象創(chuàng)建的實(shí)例中。
是的,如果您將屬性分配給函數(shù)本身,那么它將顯示使用in
function test(){
}
test.prototype.x = 3;
test.something = 5
console.log(Object.keys(test)) //["something"]
console.log("something" in test) //true
添加回答
舉報(bào)