JavaScript 面向对象的程序设计(创建对象的方式/集成的方式)
属性类型:数据属性、访问器属性
数据属性:Configurable、Enumerable、Writable、value
注意:在对象上直接定义属性,则其默认值分别为:true、true、true、undefined,若用 Object.defineProperty()设置时,其默认值分别为:false、false、false、undefined。
访问器属性:Configurable、Enumerable、Get、Set。
创建对象的方式:
工厂模式:用函数封装指定接口创建对象的细节
function createPerson(name,age,job){
var obj = new Object();
obj.name = name;
obj.age = agel
obj.job = job;
obj.sayName = function(){
alert(this.name);
}
return obj;
}
var person = createPerson("Nicholas",29,"Sofeware Engineer");
缺点:无法识别创建的对象是什么类型的对象
构造函数模式
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){\
alert(this.name);
};
}
var person = new Person("Nicholas",29,"Sofeware Engineer");
与工厂模式比较:
- 没有显示地创建对象
- 直接将属性和方法赋给了 this 对象
- 没有 return 语句
注意:创建 Person 实例,必须使用 new 操作符。
优点:能够使用 instanceof 操作符识别是对象的类型。
alert(person.instanceof Object); //true
alert(person instanceof Person); //true
缺点:每个方法都在每个实例上重新创建一次,没有做到函数复用
改进:
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = sayName;
}
function sayName(){
alert(this.name);
}
var person = new Person("Nicholas",29,"Sofeware Engineer");
注意:虽然能够做到函数复用,sayName 虽然为全局变量,但实际上只能被某些对象所使用,也会造成过多的全局函数
原型模式:每个函数都有一个 prototype(原型)属性,这个对象用途是包含可以由特定类型的所有实例共享的属性和方法。
function Person(){
}
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Sofeware Engineer";
Person.prototype.sayName = function(){
alert(this.name);
};
var person1 = new Person();
person.sayName(); //"Nicholas"
var person2 = new Person();
alert(person1.sayName == person2.sayName); //true
此时 Person.prototype.constructor = Person。
有更简单的原型语法
function Person(){}
Person.prototype = {
name:"Nicholas",
age:29,
job:"Software Engineer",
sayName:function(){
alert(this.name);
}
}
但要 特别注意,此时相当于重写源性对象,Person.prototype.constructor
不等于 Person,而是 Object。
则要在 name 前显式添加 “constructor:Person”,此时会导致 constructor 的 [[Enumerable]] 为 true(默认下原生的 constructor 为 false),故可以用 Object.defineProperty 来定义。
可以通过对象实例访问保存在原型中的值,但不能通过对象实例重写原型中的值,即只能屏蔽,不能修改。
delete 操作符可以删除存在于实例中的属性,不能删除存在原型中的属性。
Object.key 、Object.getOwnPropertyNames() 和 for-in 循环,均为获取属性,包括存在于原型的属性和实例的属性。其中,Object.keys() 是通过实例调用,则返回的是实例属性,若是类调用,则返回该类可枚举属性的字符串。
缺点:不能传参数。
组合模式:目前使用最广泛、认同度最高的创建自定义对象的方法
组合模式是结合构造函数模式和原型模式,构造函数用于定义实例属性,而原型模式用于定义方法和共享的属性。
function Person(name,age) {
this.name = name;
this.age = age;
}
Person.prototype = {
constructor:Person,
sayName:function(){
alert(this.name);
}
}
动态原型模式:通过检查某个应该存在的方法是否有效,来决定是否初始化原型。
function Person(name,age) {
this.name = name;
this.age = age;
if(typeif this.sayName != "function"){
Person.prototype.sayName = function(){
alert(this.name);
};
}
}
var friend = new Person("Nicholas",29);
friend.sayName();
继承:
ECMAScript的继承方式主要是依靠原型链来实现的。
组合继承:思路是使用原型链对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。
例如:
function SuperType(name){
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function(){
alert(this.name);
};
function SubType(name,age){
// 继承属性
SuperType.call(this,name);
this.age = age;
}
//继承方法
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
alert(this.age);
};
var instance1 = new SubType("Nicholas",29);
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"Nicholas"
instance1.sayAge(); //29
var instance2 = new SubType("Grey",27);
alert(instance2.colors); //"red,blue,green"
instance2.sayName(); //"Grey"
寄生组合式继承:目前引用类型最理想的继承方式
function inheritPrototype(subType,superType){
var prototype = Object(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
}
function SuperType(name){
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function(){
alert(this.name);
};
function SubType(name,age){
// 继承属性
SuperType.call(this,name);
this.age = age;
}
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge = function(){
alert(this.age);
};