JavaScript——对象与原型链属性检测

let arr = []
console.log(arr);
console.log(arr.hasOwnProperty('length')); // 本身就有的属性 true
console.log(arr.hasOwnProperty('concat')); // 原型上的方法不能用hasOwnProperty访问 false
console.log('concat' in arr); // 原型上的方法可以用 '方法' in xx 来确定 true

 

用检测属性的方法来实现的demo

function login(options){
  if(!options.hasOwnProperty('pwd')){
    throw new Error('必须填密码')
  }
}
login({name:'test'})

 

posted @ 2020-10-29 10:03  小昱同学  阅读(164)  评论(0)    收藏  举报