function instance_of(L, R) {//L 表示左表达式,R 表示右表达式
var O = R.prototype;// 取 R 的显示原型对象
L = L.__proto__;// 取 L 的隐式原型对象
while (true) {
if (L === null)
return false;
if (O === L)// 这里重点:当 O 严格等于 L 时,返回 true
return true;
L = L.__proto__;
}
}
const reg = new RegExp(/e/i);
reg instanceof RegExg; // true
let hasOwnProperty = Object.prototype.hasOwnProperty
if (hasOwnProperty.call(obj, "foo")) {
console.log("has property foo")
}
let object = { foo: false }
Object.hasOwn(object, "foo") // true
let object2 = Object.create({ foo: true })
Object.hasOwn(object2, "foo") // false
function Person() {
}
Person.prototype.name = 'nick';
let person = new Person();
console.log('name' in person); // true
console.log(Object.prototype.hasOwnProperty(person, 'name')); // false
console.log(Object.hasOwn(person, 'name')); // false
class Person {
constructor() {
this.name = 'Nike';
}
}
let person = new Person();
console.log('name' in person); // true
console.log(Object.prototype.hasOwnProperty(person, 'name')); // false
console.log(Object.hasOwn(person, 'name')); // true