js原型链笔记

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

可正常计算的范围 小数点前 16位, 后16位。


构造函数特点 大驼峰命名法 new


原型:
1.定义(prototype):
原型是function对象的一个属性,它定义了构造函数制造出的对象的
公共祖先。通过该构造函数产生的对象,可以继承改原型的属性和方
法。原型也是对象。
2.利用原型特点和概念,可以提取公有属性。
3.对象如何查看原型-->隐式属性 __proto__ 里面指向的是原型(原型链的连接点)
4.对象如何查看对象的构造函数-->constructor (手动可以更改)

<!--a.sayName() sayName里面的this指向是,谁调用的这个方法,this结束指向谁 -->
绝大多数对象的最终都会继承自Object.prototype Object.create(null)这个不会继承自Object.prototype
Object.create(原型,例如 Person.prototype)

call/apply:
作用:改变this指向。
区别:后面传的参数形式不同。
差距:传参列表不同
call:需要吧实参按照形参的个数传进去
apply:需要传一个arguments


继承发展史:
1.传承形式-->原型链
过多的继承了没用的属性
2.借用构造函数(call/apply)
不能继承借用构造函数的原型
每次构造函数都要多走一个函数
3.共享原型 (函数.prototype = 函数.prototype)
不能随便改动自己的原型
4.圣杯模式



<script type="text/javascript">


// Person.prototype 原型
// Person.prototype = {} 是 var person = new Person(); 的祖先

Person.prototype = {
proto : 123,
};
function Person(name , age , sax) {
this.name = name;
this.age = age;
this.sax = sax;
}
var person = new Person('旺飞', 20 , 'male');

// 借用构造函数
function Person(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
function Student(name, age, sex, tel, grade) {
Person.call(this, name, age, sex);
this.tel = tel;
this.grade = grade;
}
var student = new Student('xie', 20, 'male', 111, 2019);



// 继承
// 1.圣杯模式
Father.prototype.lastname = 'deng';
function Father() {}
function Son() {this.sex = 'hehe';}
function inherit(Target, Origin) {
function F() {}
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constructor = Target;
Target.prototype.uber = Origin.prototype; // 超类
}
inherit(Son, Father);
var son = new Son();
var father = new Father();

//2. 圣杯模式TUI3 建议使用第二种

Father.prototype.lastname = 'deng';
function Father() {}
function Son() {this.sex = 'hehe';}
var inherit = (function () {
var F = function () {};
return function (Target, Origin) {
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constructor = Target;
Target.prototype.uber = Origin.prototype;
}
}());
inherit(Son, Father);
var son = new Son();
var father = new Father();
</script>


</body>
</html>
原文地址:https://www.cnblogs.com/xiewangfei123/p/12251893.html