js创建对象和继承
2014-06-23 19:49
387 查看
一、创建对象
1、组合模式(构造函数模式和原型模式):构造函数用于定义实例属性,而原型模式用于定义方法和共享属性。
2、动态原型模式
二、继承(js主要通过原型链实现继承)
1、组合继承:使用原型链实现对原型属性和方法的继承,而通过构造函数实现实例属性的继承
2、寄生组合式继承:不必为了指定子类型的原型而调用超类型的构造函数,所需要的无非就是超类型原型的一个副本。
1、组合模式(构造函数模式和原型模式):构造函数用于定义实例属性,而原型模式用于定义方法和共享属性。
function Person(name ,age,job){ this.name = name; this.age = age; this.job = job; this.friends = ['a','b','c']; } Person.prototype = { constructor : Person, sayName : function(){ alert(this.name); } }; var person1 = new Person("Nicholas", 29, "Software Engineer"); var person2 = new Person("Greg", 27, "Doctor"); person1.friends.push("Van"); alert(person1.friends); //"Shelby,Court,Van" alert(person2.friends); //"Shelby,Court" alert(person1.friends === person2.friends); //false alert(person1.sayName === person2.sayName); //true
2、动态原型模式
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 person1 = new Person("nic","20","soft");
二、继承(js主要通过原型链实现继承)
1、组合继承:使用原型链实现对原型属性和方法的继承,而通过构造函数实现实例属性的继承
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); ////第二次调用SuperType() this.age = age; } SubType.prototype = new SuperType(); //第一次调用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(instance2.colors); //"red,blue,green" instance2.sayName(); //"Greg"; instance2.sayAge(); //27
2、寄生组合式继承:不必为了指定子类型的原型而调用超类型的构造函数,所需要的无非就是超类型原型的一个副本。
function object(o){ function F(){} F.prototype = o; return new F(); } function inheritPrototype(subType, superType){ var prototype = object(superType.prototype); //create object prototype.constructor = subType; //augment object subType.prototype = prototype; //assign object } 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 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(instance2.colors); //"red,blue,green" instance2.sayName(); //"Greg"; instance2.sayAge(); //27
相关文章推荐
- JS对象的创建和继承
- JS面向对象,从创建对象到对象继承
- js创建对象的构造函数模式+原型模式和组合继承\Hybrid Pattern & combination inheritance
- 前端-JS基于原型面向对象(对象创建,继承,构造等)
- JS创建对象方法以及继承方法
- 【学习笔记六】 - js中 创建对象的模式与继承 及 js中实现块级作用域和函数私有变量 《js高程》6-7笔记
- JS面向对象,从创建对象到对象继承
- JS如何创建对象和继承对象
- js 创建对象与原型链继承
- 50 JS-4 创建对象 继承
- 理解js的对象,对象创建,继承
- js 对象创建及其继承的方法
- js中的对象创建与继承
- js创建对象与继承
- JS 中对象的简单创建和继承
- js 对象深复制,创建对象和继承
- js创建对象继承
- JS如何创建对象和继承对象
- JS创建对象与继承总结
- js加强:原型,创建对象分析,继承