一、創建對象的幾種方式
1、工廠模式:缺點是不知道對象類型
function createPerson(age) {
var o = new Object();
o.age = age
o.sayAge = function() {
alert(this.age);
}
return o;
}
var p1 = createPerson(11);
var p1 = createPerson(12);
2、構造函數模式:缺點就是每一次new Person的時候都是創建一個Function對象實例(sayAge)
function Person(age) {
this.age = age;
this.sayAge = function() {
alert(this.age);
}
}
var p1 = new Person(11);
var p2 = new Person(12);
3、原型模式:缺點數據共享的問題
hasOwnProperty可以檢測屬性是實例屬性還是原型中的屬性;
in:對象能夠訪問屬性的時候返回true;
function Person() {
}
Person.prototype={
constructor:Person,
name:"gt",
friends:['a','b'],
sayName:functon(){
alert(this.name);
}
};
var p1 = new Person();
var p2 = new Person();
p1.friends.push('c');
這樣p1和p2都有朋友c這就是他們的問題所在
4、組合使用構造函數模式和原型模式
目前使用的最爲廣泛的一種模式
function Person(age) {
this.age=age;
}
Person.prototype = {
constructor:Person,
sayAge:function(){
alert(this.age);
}
}
var p1 = new Person(11);
var p2 = new Person(12);
5、動態原型模式
function Person(age) {
this.age=age;
if(typeof this.sayName != "function") {
Person.portotype.sayName = function() {
alert(this.name)
}
}
}
var p1 = new Person(11);
6、寄生構造函數模式
function SpecialArray() {
var values = new Array();
values.push.apply(values,arguments);
values.toPipedString = function() {
return this.join('|');
}
return values;
}
var colors = new SpecialArray('a','b');
7、穩妥構造函數模式:沒有公共屬性,而且其方法也沒引用this的對象
function Person(age){
var o = new Object();
o.sayAge = function() {
alert(name);
}
return o;
}
var p1 = new Person(11);]
p1.sayAge();
只能通過sayAge方式來訪問name,多用於比較安全的環境中
二、繼承(主要通過原型鏈來實現)
1、原型鏈
function SuperType(){
this.property = true;
}
SuperType.propotype.getSuperValue = function(){
return this.property;
};
function SubType(){
this.subProperty=false;
}
SubType.prototype = new SuperType();
SubType.propotype.getSubTypeValue = function(){
return this.subProperty;
}
var instance = new SubType();
alert(instance.getSuperType());
2、借用構造函數
function SuperType(name){
this.name = name;
}
function SubType(){
SuperType.call(this,"nnnn");
this.age=20;
}
var instance = new SubType();
3、組合繼承
function SuperType(name){
this.name= name;
this.colors = [1,2,3]
}
SuperType.propotype.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.propotype.sayAge= function(){
alert(this.age);
}
var instance = new SubType('aaaa',20);
instance.colors.push(4);
4、原型式繼承
function object(0) {
function F(){}
F.prototype=o;
return new F();
}
var person = {
name='1111';
friends=[1,3];
}
var p1 = object(person);
var p2 = object(person);
friends會被共享
//用Object.create方法實現
var p1 = Object.create(person);
var p2 = Object.create(person);
5、寄生式繼承
function object(0) {
function F(){}
F.prototype=o;
return new F();
}
function createAnother(original) {
var clone = object(original);
clone.sayHi = function() {
alert('hi');
}
}
var person = {
name='1111';
friends=[1,3];
}
var p1 = createAnother(person);
p1.sayHi();
6、寄生組合式繼承
function object(0) {
function F(){}
F.prototype=o;
return new F();
}
function inheritPrototype(subType,SuperType) {
var prototype=object(super.prototype);
prototype.constructor = subType;
subType.prototype=prototype;
}
function SuperType(name){
this.name = name;
this.colors = [1,2,3,4];
}
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);
}