个人总结: call 继承的是父类私有
prototype 继承的父类公有
create 可以将公有或私有继承到子类上去 (克隆)
for in 克隆 不管公有还是私有的都克隆成私有的
1. 原型继承: 将父类的私有和公有都继承子类的原型上. 子类的原型等于父类的实例.(私有公有全部继承)
- function A() {
- this.name='aaa'
- }
- A.prototype.x=50;
- function B() {
- this.age='bbb'
- }
- B.prototype.y=100;
- B.prototype=new A;
- var a=new A;
- var b=new B;
- console.log("xsx------="+b.x); //50
- console.log("xsx------="+b.name);//aaa
2.call 继承: 将父类的私有继承子类的私有.(prototype 为公有)
- function A() {
- this.name='aaa'
- }
- A.prototype.x=50;
- function B() {
- this.age='bbb'
- A.call(this)
- }
- B.prototype.y=100;
- var a=new A;
- var b=new B;
- console.log("xsx------="+b.x); //undefined
- console.log("xsx------="+b.name); //aaa
3. 冒充对象继承: 将父类的私有和公有都继承子类私有的.
- function A() {
- this.name = 'aaa'
- }
- A.prototype.x = 50;
- function B() {
- this.age = 'bbb'
- var teme = new A
- for (var k in teme) {
- this[k]=teme[k]
- }
- }
- B.prototype.y = 100;
- var a = new A;
- var b = new B;
- console.log("xsx------=" + b.x); //50
- console.log("xsx------=" + b.name);//aaa
4. 混合继承: 将父类私有继承子类私有, 再将父类的私有和公有继承子类公有. 采用 call 继承和原型继承, 私有被继承两次.
- function A() {
- this.name = 'aaa'
- }
- A.prototype.x = 50;
- function B() {
- this.age = 'bbb'
- A.call(this) //call 是用来继承私有的
- }
- B.prototype.y = 100;
- B.prototype = new A;
- var a = new A;
- var b = new B;
- console.log("xsx------=" + b.name);//aaa
5. 组合继承: 私有继承私有, 公有继承公有
- function A() {
- this.name = 'aaa'
- }
- A.prototype.x = 50;
- function B() {
- this.age = 'bbb'
- A.call(this) //call 是用来继承私有的
- }
- B.prototype =Object.create(A.prototype); // 继承私有
- B.prototype.y = 100;
- var a = new A;
- var b = new B;
- console.log("xsx------=" + b.name);//aaa
来源: http://www.bubuko.com/infodetail-2911724.html