手写系列– 原型和原型链

58次阅读

共计 1048 个字符,预计需要花费 3 分钟才能阅读完成。

网址
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>
画图

打印

正文完
 0