继承模式
模式1:默认模式
代码复用大家常用的默认模式,往往是有问题的,该模式使用Parent()的构造函数创建一个对象,并且将该对象赋值给Child()的原型。我们看一下代码:
- function inherit(C, P) {
- C.prototype = new P();
- }
- function Parent(name) {
- this.name = name || 'Adam';
- }
- Parent.prototype.say = function () {
- return this.name;
- };
- function Child(name) {
- }
- inherit(Child, Parent);
- var kid = new Child();
- console.log(kid.say()); // "Adam"
- var kiddo = new Child();
- kiddo.name = "Patrick";
- console.log(kiddo.say()); // "Patrick"
- var s = new Child('Seth');
- console.log(s.say()); // "Adam"
缺点 1.同时继承了两个对象的属性,即添加到this的属性和原型的属性. 2.Child不能传进参数。
模式2:借用构造函数
该模式是Child借用Parent的构造函数进行apply,然后将child的this和参数传递给apply方法:
- function Child(a,b,c,d){
- Parent.apply(this,arguments);
- }
- function Parent() {
- this.name = [1,2,3];
- }
- var parent = new Parent();
- Parent.prototype.say = function () {
- return this.name;
- };
- //pattern 2
- function Child_2() {
- Parent.apply(this, arguments);
- }
- //pattern1
- function Child_1(){
- }
- Child_1.prototype = parent;
- var kid_pattern1 = new Child_1();
- var kid_pattern2 = new Child_2();
- console.log(parent.hasOwnProperty("name"));//true
- console.log(kid_pattern1.hasOwnProperty("name"));//false
- console.log(kid_pattern2.hasOwnProperty("name"));//true
- console.log(typeof kid_pattern1.say); // "function"
- console.log(typeof kid_pattern2.say); // "undefined"
- kid_pattern1.name.push("changeBYPattern1");
- kid_pattern2.name.push("changeBYPattern2");
- console.log(parent.name);//[1, 2, 3, "changeBYPattern1"]
缺点 1.只继承了this对象上的属性,并没有继承到原型的属性(say()),
区别 1.模式2可以获得父对象中name属性的副本,而模式1获得的是父对象中name属性的引用
模式3:借用构造函数并设置原型
上述两个模式都有自己的缺点,那如何把两者的缺点去除呢,我们来尝试一下:
- function Parent(name) {
- this.name = name || 'Adam';
- }
- Parent.prototype.say = function () {
- return this.name;
- };
- function Child(name) {
- Parent.apply(this, arguments);
- }
- Child.prototype = new Parent();
- var kid = new Child("Patrick");
- console.log(kid.name); // "Patrick"
- console.log(typeof kid.say); // function
- console.log(kid.say()); // Patrick
- console.dir(kid);
- delete kid.name;
- console.log(kid.say()); // "Adam"
缺点 1.Parent构造函数执行了两次,所以说,虽然程序可用,但是效率很低。
模式4:共享原型
共享原型是指Child和Parent使用同样的原型,代码如下:
- function inherit(C, P) {
- C.prototype = P.prototype;
- }
- function Parent(name) {
- this.name = name || 'Adam';
- }
- Parent.prototype.say = function () {
- return this.name;
- };
- function Child(name) {
- }
- inherit(Child, Parent);
- var kid = new Child('Patrick');
- console.log(kid.name); // undefined
- console.log(typeof kid.say); // function
- kid.name = 'Patrick';
- console.log(kid.say()); // Patrick
- console.dir(kid);
缺点 1.Child的参数没有正确接收到. 2.如果子对象修改了原型会影响到所有的祖先对象.
模式5:临时构造函数
首先借用构造函数,然后将Child的原型设置为该借用构造函数的实例,最后恢复Child原型的构造函数。代码如下:
- var inherit = (function () {
- var F = function () {
- };
- return function (C, P) {
- F.prototype = P.prototype;
- C.prototype = new F();
- C.uber = P.prototype;//存储超类(super是保留的关键字)
- C.prototype.constructor = C;//重置构造函数,如果不重置,子对象的构造函数会指向Parent
- }
- } ());
- function Parent(name) {
- this.name = name || 'Adam';
- }
- Parent.prototype.say = function () {
- return this.name;
- };
- function Child(name) {
- }
- inherit(Child, Parent);
- var kid = new Child("Patrick");
- console.log(kid.name); // undefined
- console.log(typeof kid.say); // function
- kid.name = 'Patrick';
- console.log(kid.say()); // Patrick
- console.log(kid.constructor.name); // Child
- console.log(kid.constructor === Parent); // false
为了避免每次需要继承时都要创建一个代理构造函数可以使用闭包来储存,进行优化
- var inherit = (function(){
- var F = function () {};
- return function(C,P){
- F.prototype = P.prototype;
- C.prototype = new F();
- C.uber = P.prototype;
- C.prototype.constructor = C;
- }
- })()
缺点 1.Child不能正常接收参数.
区别 1.和模式1的区别在于,子类对象只继承了原型对象上的成员,而没有继承到this对象的成员,这也正是我们所需要的.因为原型上的才是可复用的
模式6:klass
- var klass = function (Parent, props) {
- var Child, F, i;
- Child = function () {
- if (Child.uber && Child.uber.hasOwnProperty("__construct")) {
- Child.uber.__construct.apply(this, arguments);
- }
- if (Child.prototype.hasOwnProperty("__construct")) {
- Child.prototype.__construct.apply(this, arguments);
- }
- };
- Parent = Parent || Object;
- F = function () {
- };
- F.prototype = Parent.prototype;
- Child.prototype = new F();
- Child.uber = Parent.prototype;
- Child.prototype.constructor = Child;
- for (i in props) {
- if (props.hasOwnProperty(i)) {
- Child.prototype[i] = props[i];
- }
- }
- // return the "class"
- return Child;
- };
- var Man = klass(null, {
- __construct: function (what) {
- console.log("Man's constructor");
- this.name = what;
- },
- getName: function () {
- return this.name;
- }
- });
- var first = new Man('Adam'); // logs "Man's constructor"
- first.getName(); // "Adam"
- var SuperMan = klass(Man, {
- __construct: function (what) {
- console.log("SuperMan's constructor");
- },
- getName: function () {
- var name = SuperMan.uber.getName.call(this);
- return "I am " + name;
- }
- });
- var clark = new SuperMan('Clark Kent');
- clark.getName(); // "I am Clark Kent"
- console.log(clark instanceof Man); // true
- console.log(clark instanceof SuperMan); // true
缺点 1.看起来十分混乱.
posted on 2013-10-23 18:04 rainbow661314 阅读(193) 评论(0) 编辑 收藏 举报