<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// 构造函数: 是否为构造函数 看它是否创建实例
function Person() {}
Person.prototype.name = "涂涂";
Person.prototype.age = 7;
Person.prototype.getAge = function () {
console.log(this.age, 111);
};
let person1 = new Person();
console.log(person1, 222);
// Person
// __proto__:
// age: 7
// getAge: ƒ ()
// name: "涂涂"
// constructor: ƒ Person()
// __proto__: Object
// constructor : 构造器 指向构造他的函数
console.log(person1.constructor, 333); // ƒ Person() {} 333
person1.name = "宁宁";
console.log(person1.name, 44444444444); // 宁宁 44444444444
// 原型链查找规则 -proto-原型链
// 沿着原型链 (-proto-) 一层一层网上找,知道找到null为止 如果没有找到 报未定义
// null => Object ,Array(基类)
person1.a = "基本属性";
console.log(person1, 55555);
// 循环判断 hasOwnProperty
let item;
for (item in person1) {
if (person1.hasOwnProperty(item)) {
console.log(item,666);
}
}
</script>
</body>
</html>
原型链与继承
最新推荐文章于 2022-05-27 15:20:05 发布