1、原始方法
let obj = {
id: 7,
name: 'function',
fn: function () {
return this.id;
},
test: function test(params) {
// 旧的判断方法
return Object.prototype.hasOwnProperty.call(obj, params);
}
};
console.log(obj.test('fn'));
// true
console.log(obj.test('a'));
// false
console.log(obj.fn());
// 7
2、新方法
let obj = {
id: 7,
name: 'function',
fn: function () {
return this.id;
},
test: function test(params) {
// 新方法
return Object.hasOwn(obj, params);
}
};
console.log(obj.test('fn'));
// true
console.log(obj.test('a'));
// false
console.log(obj.fn());
// 7