网址
https://github.com/mqyqingfen...
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
function Person(name, age) {
this.name = name;
this.age = age;
this.say = function() {
alert(this.name + "的年龄是" + this.age)
}
}
Person.prototype.name = "admin"
var p1 = new Person('jie', 10);
var p2 = new Person('biao', 20)
delete p1.name;
console.log(p1.name)
console.log(p2.name)
//以原型链为线索
console.log(p1.__proto__ === Person.prototype)
console.log(Person.prototype.__proto__ === Object.prototype)
console.log(Object.prototype.__proto__ === null)
//以Person为线索
console.log(Person.__proto__ === Function.prototype)
console.log(Person === Person.prototype.constructor)
//以构造函数为线索
console.log(p1.constructor)
console.log(Person.constructor)
console.log(Function.constructor)
console.log(Object.constructor)
//以Function为线索
console.log(Person.__proto__ === Function.prototype)
console.log(Function.prototype.__proto__ === Object.prototype)
</script>
</body>
</html>
画图
打印
instanceof的实现
简介
instanceof:实例
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上
示例
function Test (name) {
this.name = name
this.log = function () {
console.log('this.name', this.name)
}
}
const t = new Test()
console.log(t instanceof Test)
源码实现
function myInstanceof (obj, constructor) {
let obj__proto__ = obj.__proto__
while (true) {
if (obj__proto__ === null) {
return false
}
if (obj__proto__ === constructor.prototype) {
return true
}
obj__proto__ = obj__proto__.__proto__
}
}
const tt = new Test()
console.log(myInstanceof(tt, Test))
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。