Skip to content

深入扩展原型链模式常用的六种继承方式 #15

Open

Description

@Yangxiaqing

不可枚举和可枚举

for in 循环在遍历的时候,默认的话可以把自己私有的和在它所属类原型上扩展的属性和方法都可以遍历到,但是一般情况下,我们遍历一个对象只需要遍历私有的即可,我们可以使用以下的判断进行处理:

Object.prototype.aaa = function () {

};
var obj = {
	name: "yang",
	age: 18,
};
for (var key in obj) {
	//if(obj.propertyIsEnumerable(key)){
		//console.log(key);
	//}
	// 或者写成:
	if(obj.hasOwnProperty(key)){
		console.log(key);
	}
}

Object.create

Object.create() 方法创建一个拥有指定原型和若干个指定属性的对象。
Object.create(proObj) 创建一个新的对象,但是还要把proObj作为这个对象的原型
在IE6~8不兼容(ECMAScript5)

var obj = {getX: function() {}};
var obj2 = Object.create(obj);
//--->obj2
//__proto__:
// getX: function...
// __proto__: Object.prototype
obj2.getX();
obj.getY = function () {
    console.log(2);
};
obj2.getY();

原理:

var obj = {
    getX: function() {
 
    }
};
function object(o) {
    function Fn() {
 
    }
    Fn.prototype = o;
    return new Fn;
}
var newObj = object(obj);

原型继承(最重要)

function A() {
	this.x = 100;
}
A.prototype.getX = function () {
	console.log(this.x);
};

function B() {
	this.y = 200;
}
B.prototype  = new A;
  • 原型继承是我们JS中最常用的一种继承方式
  • 子类B想要继承父类A中的所有的属性和方法(私有+公有),只需要让B.prototype = new A即可。
  • 原型继承的特点:
    它把父类中私有的+公有的都继承到了子类原型上(子类公有的)
  • 核心:原型继承并不是把父类中的属性和方法克隆一份一模一样的给B,而是让B和A之间增加了原型链的连接
    image

call继承:

把父类私有的属性和方法 克隆一份一模一样的 作为子类私有的属性

function A() {
    this.x = 100;
}
A.prototype.getX = function () {
    console.log(this.x);
};
 
function B() {
    // this--->n
    A.call(this);// A.call(n) 把A执行让A中的this变成了n
}
 
var n = new B;
console.log(n.x);

冒充对象继承:

把父类私有的+公有的,克隆一份一模一样的,给子类私有

function A() {
	this.x = 100;
}
A.prototype.getX = function () {
	console.log(this.x);
};

function B() {
	// this   --->n
	var temp = new A;
	for (var key in temp) {
		this[key] = temp[key];
	}
        temp = null;
}
var n = new B;

混合模式继承:

原型继承+call继承

function A() {
	this.x = 100;
}
A.prototype.getX = function () {
	console.log(this.x);
};

function B() {
	// n.x= 100
	A.call(this);
}
B.prototype = new A;
//B.prototype: x = 100
B.prototype.constructor = B;

var n = new B;
n.getX();

寄生组合式继承

function A() {
	this.x = 100;
}
A.prototype.getX = function () {
	console.log(this.x);
};

function B() {
	A.call(this);
}
B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;

var n = new B;
console.log(n);

function objectCreate(o) {
	function fn() {

	}
	fn.prototype = o;
}

中间类继承法(不兼容)

function avgFn() {
	arguments.__proto__ = Array.prototype;
	arguments.sort(function (a,b) {
		return a-b;
	});
	arguments.pop();
	arguments.shift();
	return (eval(arguments.join.("+")) / arguments.length;
}
console.log(avgFn(10,20,30,10,30,40,40));

Activity

  1. added a commit that references this issue on Mar 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions