JS创建对象跟继承的方式

JS创建对象和继承的方式

对象创建:

1.

//以下三种方法创建的对象是一样的,都是一个最普通的Object对象
var object = {}
var object = new Object();
var object = Object.create(Object.prototype);

 创建的对象如图:JS创建对象跟继承的方式

 

2.

/*创建一个没有原型的空对象
 *这个新建的对象很可怜,他没有任何的原型
 *也就是说它连Object内置的方法都没有,不能toString(),valueOf等
 */
var object = Object.create(null);

  创建的对象如图:JS创建对象跟继承的方式

 

3.

var point = { x : 1, y : 1}

  创建的对象如图:JS创建对象跟继承的方式

 
 4.

var point = Object.create({ x : 1, y : 1});	//以{x:1,y:1}为原型创建一个对象

  创建的对象如图:JS创建对象跟继承的方式

 

5.创建类:

   方式一

function Person(name, sex, age){
	this.name = name;
	this.sex = sex;
	this.age = age;
	
	this.say = function(){
		alert("My name is " + name);
	}
}

   方式二

var Person = {
	_name : null,
	_sex : null,
	_age : null,

	create : function(name, sex, age){
		var self = Object.create(this);
		self.setName(name);
		self.setSex(sex);
		self.setAge(age);

		return self;
	},
	say : function(){
		alert("My name is " + this.getName());
	},

	setName : function(name){
		this._name = name;
	},
	getName : function(){
		return this._name;
	},

	setSex : function(sex){
		this._sex = sex;
	},

	getSex : function(){
		return this._sex;
	},

	setAge : function(age){
		this._age = age;
	},
	
	getAge : function(){
		return this._age;
	}
}

 

对象继承:

 方式一:对象冒充(它们实质上是改变了this指针的指向)

    Person父类:

function Person(name, sex, age){
	this.name = name;
	this.sex = sex;
	this.age = age;
	
	this.say = function(){
		alert("My name is " + name);
	}
}

    1.Student子类(临时属性方式)

function Student(name, sex, age, stuNo){
	this.temp = Person;
	this.temp(name, sex, age);
	delete this.temp;
	
	this.stuNo = stuNo;
	this.show = function(){
		this.say();
		alert("My stuNo is " + stuNo);
	}
}

    2.Teacher子类(call()及apply()方式)

function Teacher(name, sex, age, subject){
	Person.call(this, name, sex, age);	//apply()方式改成Person.apply(this,new Array(name,sex,age,subject));
	
	this.subject = subject;
	this.show = function(){
		this.say();
		alert("I'm a teacher");
	}
}

 方式二:混合方式(成员变量采用对象冒充方式,成员方法采用原型链方式)

    Person父类:

function Person(name, sex, age){
	this.name = name;
	this.sex = sex;
	this.age = age;
}
Person.prototype.say = function(){
	alert("My name is " + this.name);
}
    President子类:
function President(name, sex, age, university){
	Person.call(this, name, sex, age);	//拷贝属性(通过this调用Person函数)
	this.university = university;
}
President.prototype = new Person();		//拷贝方法,原型指向实例(如果原型指向原型,则在子类中添加方法就相当于在父类中添加,如下面的show方法就会被直接添加到父类中)
//show方法的声明要写在President.prototype = new Person()后面
President.prototype.show = function(){
	alert("I'm the president of " + this.university + " University");
}
    测试:
var person = new Person("Janny", "female", 24);
//person.show();
person.say();
var president = new President("Same", "male", 54, "Yale");
president.show();
president.say();
 方式三:其它
var Person = Object.create(null,{	//创建一个Person类
	name : {value : null},
	sex : {value : null},
	age : {value : null},
	say : {value : function(){
		alert("My name is " + this.name);
	}}
});

var student = Object.create(Person, {	//创建一个student实例
	name : {value : "Tom"},
	sex : {value : "male"},
	age : {value : 22}
});
student.say();

var teacher = Object.create(Person, {	//创建一个teacher实例
	name : {value : "Jake"},
	sex : {value : "male"},
	age : {value : 34},
	show : {value : function(){
		alert("I'm a Teacher");
	}}
});
teacher.show();
teacher.say();
			
var Developer = Object.create(Person,{	////创建一个Developer类
	language : {value : null},
	show : {value : function(){
		alert("I'm a " + this.language + " programmer");
	}}
});
var deve = Object.create(Developer,{	//创建一个developer实例
	name : {value : "Ben"},
	sex : {value : "male"},
	age : {value : 28},
	language : {value : "Java"}
});
deve.say();
deve.show();
 
var Person = Object.create(null,{
	name : {value : null},
	sex : {value : null},
	age : {value : null},
	say : {value : function(){
		alert("My name is " + this.name);
	}}
});

var Student = function(_name, _sex, _age){	//这样创建一个类,可以使用new创建实例,但它将不再适合拥有子类
	return Object.create(Person, {
		name : {value : _name},
		sex : {value : _sex},
		age : {value : _age},
		show : {value : function(){
			alert("I'm a Student");
		}}
	});
}
var stu = new Student("Janny", "female", 24);	//new关键字创建实例效率较低
stu.say();