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'})

浙公网安备 33010602011771号