理解对象
var person = new Object();
person.name ="Nicholas";
person.age = 29;
person.job ="SoftWar Engineer";
person.sayName = function(){
alert(this.name);
}
用对象字面量语法:
var person = {
name:"Nicholas",
age:29,
job:"SoftWar Engineer",
sayName:function(){
alert(this.name);
}
}
创建对象
工厂模式:
function createPerson(name,age,job){
var o = new Object();
o.name = name;
o.job = job;
o.sayName = function(){
alter(this.name);
};
return o;
}
var person1 = Person("nicholas",25,"soft enginewr");
构造函数模式:
function Person(name,age,job){
this.name = name;
this.age = age;
this.sayName = function(){
alter(this.name);
};
}
var person1 = new Person("nicholas",25,"soft enginewr");
原型模式:
function Person(){
}
Person.prototype.name = "nicholas";
Person.prototype.age = "22";
Person.prototype.job = "soft enginewr";
Person.prototype.sayName = function(){
alert(this.name);
}
var person1 = new Person();
person1.sayName();//"nicholas"
var person2 = new Person();
person2.sayName();//"nicholas"
alert(person1.sayName == person2.sayName); //true
组合使用构造函数模式和原型模式:
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.friends = ["xx1","xx2"];
}
Person.prototype = {
constructor = Person;
sayName: function(){
alert(this.name);
}
}
var person1 = new Person("nicholas",25,"soft enginewr");
动态原型模式:
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
if(typeof this.sayName != "function"){
Person.prototype.sayName = function(){
alert(this.name);
};
}
}
var fridend = new Person("nicholas",25,"soft enginewr");
friend.sayName();
寄生构造函数模式:
function Person(name,age,job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
alter(this.name);
}
return o;
}
var fridend = new Person("nicholas",25,"soft enginewr");
稳妥构造函数模式:
function Person(name,age,job){
var o =new Object();
o.sayName = function(){
alert(name);
};
return o;
}
原型链:
function SuperType(){
this.propety = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
};
function SubType(){
this.subproperty = false;
}
//继承SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function(){
return this.subproperty;
};
var instacne = new SubType();
alert(instance.getSuperValue());//ture
借用构造函数:
function SuperType(){
this.colors = ["red","blue","green"];
}
function SubType()}{
//继承SuperType
SuperType.call(this);
}
var instance1 = new SubType();
instance1.colors.push("black");
alert(insctance1.colors);//"red,blue,green,black"
var instance2 = new SubType();
alert(instance2.colors);//"red,blue,green"
组合继承:
function SuperType(name){
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function(name){
alter(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("GREG",27);
alert(instance1.colors);//"red,blue,green"
instance1.sayName();//"GREG"
instance1.sayAge();//27
原型式继承:
function object(o){
function F();
F.Prototype = o;
return new F();
}
var person = {
name :"nicholas",
friends:["shelby","court","van"]
};
var anotherPerson = object(person);//var anotherPerson = 0bject.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("rob");
var yetAnotherPerson = object(person);
yetAnotherPersonname = "Linda";
yetAnotherPerson.friends.push("barbie");
alert(person.friends);//"shelby,court,van,rob,barbie"
寄生式继承:
function createAnother(original){
var clone = object(original);// 通过调用函数创建一个新对象
clone.sayHi = function(){//以某种方式来增强这个对象
alert("hi");
};
return clone; //返回这个对象
}
var person = {
name :"nicholas",
friends:["shelby","court","van"]
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();//"hi"
寄生组合式继承:
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);
}
var person = new Object();
person.name ="Nicholas";
person.age = 29;
person.job ="SoftWar Engineer";
person.sayName = function(){
alert(this.name);
}
用对象字面量语法:
var person = {
name:"Nicholas",
age:29,
job:"SoftWar Engineer",
sayName:function(){
alert(this.name);
}
}
创建对象
工厂模式:
function createPerson(name,age,job){
var o = new Object();
o.name = name;
o.job = job;
o.sayName = function(){
alter(this.name);
};
return o;
}
var person1 = Person("nicholas",25,"soft enginewr");
构造函数模式:
function Person(name,age,job){
this.name = name;
this.age = age;
this.sayName = function(){
alter(this.name);
};
}
var person1 = new Person("nicholas",25,"soft enginewr");
原型模式:
function Person(){
}
Person.prototype.name = "nicholas";
Person.prototype.age = "22";
Person.prototype.job = "soft enginewr";
Person.prototype.sayName = function(){
alert(this.name);
}
var person1 = new Person();
person1.sayName();//"nicholas"
var person2 = new Person();
person2.sayName();//"nicholas"
alert(person1.sayName == person2.sayName); //true
组合使用构造函数模式和原型模式:
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.friends = ["xx1","xx2"];
}
Person.prototype = {
constructor = Person;
sayName: function(){
alert(this.name);
}
}
var person1 = new Person("nicholas",25,"soft enginewr");
动态原型模式:
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
if(typeof this.sayName != "function"){
Person.prototype.sayName = function(){
alert(this.name);
};
}
}
var fridend = new Person("nicholas",25,"soft enginewr");
friend.sayName();
寄生构造函数模式:
function Person(name,age,job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
alter(this.name);
}
return o;
}
var fridend = new Person("nicholas",25,"soft enginewr");
稳妥构造函数模式:
function Person(name,age,job){
var o =new Object();
o.sayName = function(){
alert(name);
};
return o;
}
原型链:
function SuperType(){
this.propety = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
};
function SubType(){
this.subproperty = false;
}
//继承SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function(){
return this.subproperty;
};
var instacne = new SubType();
alert(instance.getSuperValue());//ture
借用构造函数:
function SuperType(){
this.colors = ["red","blue","green"];
}
function SubType()}{
//继承SuperType
SuperType.call(this);
}
var instance1 = new SubType();
instance1.colors.push("black");
alert(insctance1.colors);//"red,blue,green,black"
var instance2 = new SubType();
alert(instance2.colors);//"red,blue,green"
组合继承:
function SuperType(name){
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function(name){
alter(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("GREG",27);
alert(instance1.colors);//"red,blue,green"
instance1.sayName();//"GREG"
instance1.sayAge();//27
原型式继承:
function object(o){
function F();
F.Prototype = o;
return new F();
}
var person = {
name :"nicholas",
friends:["shelby","court","van"]
};
var anotherPerson = object(person);//var anotherPerson = 0bject.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("rob");
var yetAnotherPerson = object(person);
yetAnotherPersonname = "Linda";
yetAnotherPerson.friends.push("barbie");
alert(person.friends);//"shelby,court,van,rob,barbie"
寄生式继承:
function createAnother(original){
var clone = object(original);// 通过调用函数创建一个新对象
clone.sayHi = function(){//以某种方式来增强这个对象
alert("hi");
};
return clone; //返回这个对象
}
var person = {
name :"nicholas",
friends:["shelby","court","van"]
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();//"hi"
寄生组合式继承:
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);
}