让学习“上瘾”,成为更好的自己!!!
<!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>组合使用构造函数和原型模式</title>
</head>
<body>
<script>
// 【重要!!】
// 目前ECMAScript使用最广泛的创建自定义类型的方法!!!
// 创建自定义类型的最常见方式 --> 组合使用原型构造函数模式和原型模式
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.friends = ['Kai', 'Paul'];
}
// 构造函数模式用于定义实例属性
// 原型模式用于定义方法和共享的属性
Person.prototype = {
constructor:Person,
sayName: function(){
console.log(this.name);
}
};
var person1 = new Person('Lice', 29, 'teacher');
var person2 = new Person('goolge', 35, 'doctor');
person1.friends.push('Van');
console.log(person1.friends);
console.log(person2.friends);
console.log(person1.sayName === person2.sayName);
</script>
</body>
</html>```