优化性能后的构造函数
protype特点:
1.1 存储在protype中的方法可以被对应构造函数创建出来的所有对象共享;
1.2 protype除了可以存储方法外,还可以存储属性
1.3 protype中如果出现了和对象同名的属性和方法,对象在访问的时候,访问到的是构造函数中的数据。
2.1 protype中一般用来存储所有对象都相同的一些属性和方法
2.2 如果是对象特有的属性或方法,就存储到构造函数中
<!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>
<script>
function Person(myName, age, job){
this.name = myName;
this.job = job;
this.age = age;
this.currentType = "构造函数中的type";
this.say = function(){
console.log("构造函数中的say");
}
this.speak = "我在speak";
}
Person.prototype = {
currentType:"原型中的type",
say:function(){
console.log("hello world !");
}
}
let person1 = new Person("Marvin",21,"farmer");
let person2 = new Person("James",15,"begger");
// console.log(person1.say==person2.say);
console.log(person1.currentType);
console.log(person1.speak);
</script>
</head>
<body>
</body>
</html>