js原型、原型链、call apply
原型
1.定义:原型是function对象的一个属性,它定义了构造函数构造出的对象的共同祖先,经过这个构造函数建立的对象能够继承该原型的属性和方法。原型也是对象。 2.利用原型的特色和概念,能够提取共有属性。 3.对象如何查看原型:隐式属性 .proto 4.对象如何查看对象的构造函数:.constructor, 能够手动更改java
function Person(){ } car.prototype = { made:"china", price:2300000 } car.prototype.name = "BWM"; //新增原型属性 car.prototype.year = 2018; function car(color,owner){ this.color = color; this.owner = owner; } var car1 = new car("red","chen"); var car2 = new car("green","li"); console.log(car1.constructor);//function car(){}; car.prototype.constructor = Person(); console.log(car1.constructor);//function Person(){};
原型链
- 如何构成原型链
grand.prototype.__prototype__ = Object.proto grand.prototype.lastname = "chen"; function grandtype; } var grand1 = new grand(); father.prototype = grand; function father(){ this.name = "ying"; this.forturn = { card1 :"visa" } } var fa1 = new father(); son.prototype = fa1; function son(){ this.name = 'jie'; this.age = 18; } var son1 = new son();
- 原型链上的增删改查
- 后代不能增删改原型链上祖先的属性
son1.forturn.card2 = "china"; //能够添加这个属性是由于,son1在这调用了forturn,引用值forturn能够添加属性。 //可是son.prototype.wife = "hehe";能够给son.prototype添加属性wife,可是son原来的原型father上不会添加wife属性。
- 查询属性 先在本身的属性里面找,若是没有就到原型上找,若是没有再往原型的原型上找,以此类推,知道原型链尽头(Object.prototype). PS:谁调用的方法,里面的this就指向谁。
Person.prototype = { name:"a", sayName:function (){ console.log(this.name); } } function Person() { this.name = "b"; } var p = new Person(); p.sayName();//b
-
绝大多数对象最终都会继承自Object.prototype 例外:一切对象的原型是一个对象或者null,因此不是全部对象的最终都会继承自Obeject.prototype. Object.create(null);数组
-
Object.create方法app
// var obj = Object.create(原型);//原型归类 var obj = {name:"chen", age:12}; var obj1 = Object.create(obj); Person.prototype.name = "sunny"; function Person(){ } var person = Object.create(Person.prototype);//
PS: undedinefed,null是原始值,且不能进行包装类,因此它们没有toString()方法。函数
call/apply
做用:改变this 指向
- call
function Person(name,age){ this.name = name; this.age = age; } var person = new Person("chen",18); var obj = {}; Person.call(obj,"li",20);//this指向obj,第一个参数表明指向的对象,后面的参数对应到函数的参数。
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('sunny',123,'male',139,2017);
2.apply 做用:改变this指向 与call的不一样:传参列表不一样。call须要把实参按照形参的个数传进去。apply须要传一个arguments。this
function Person(name,age,sex){ this.name = name; this.age = age; this.sex = sex; } function Student(name,age,sex,tel,grade){ Person.apply(this,[name,age,sex]);//apply传进的参数为一个数组 this.tel = tel; this.grade = grade; } var student = new Student('sunny',123,'male',139,2017);