当前位置 : 主页 > 网络编程 > JavaScript >

for in & hasOwnProperty & prototype

来源:互联网 收集:自由互联 发布时间:2021-06-28
for-in.js // for in hasOwnProperty prototypefunction P(){ this.name = 'sigma';}P.prototype.age = 18;var a = new P();// code 1for( let key in a){ console.log( key );}// code 2for (let key in a) { a.hasOwnProperty( key ) console.log(key);}` 伪
for-in.js
// for in  & hasOwnProperty & prototype

function P(){
  this.name = 'sigma';
}
P.prototype.age = 18;

var a = new P();
// code 1

for( let key in a){
  console.log( key );
}

// code 2
for (let key in a) {
  a.hasOwnProperty( key ) && console.log(key);
}`
伪造hasOwnProperty
function P(){
  this.name = 'sigma';
}
P.prototype.age = 18;

P.prototype.hasOwnProperty = function( ){ // 伪造
    return false;
}
var a = new P();

for( let key in a){
  a.hasOwnProperty( key ) && console.log(key);
}
网友评论