如何使用jQuery扩展实现多重继承 [英] How to implement multiple inheritance using jQuery extend

查看:91
本文介绍了如何使用jQuery扩展实现多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Douglas Crockford的说法,我可以使用 http://javascript.crockford.com/prototypal.html (稍微调整一下)......但我对jQuery的做法很感兴趣。使用$ .extend是不错的做法?

According to Douglas Crockford I could use something like http://javascript.crockford.com/prototypal.html (with a little bit of tweaking)... but I am interested in jQuery way of doing it. Is it good practice using $.extend ?

我有4个班级:

            var A = function(){ } 
            A.prototype = {
                name : "A",
                cl : function(){
                    alert(this.name);
                }
            }
            var D = function(){} 
            D.prototype = {
                say : function(){
                    alert("D");
                }
            }

            var B = function(){} //inherits from A
            B.prototype = $.extend(new A(), {
                name : "B"
            });

            var C = function(){} //inherits from B and D
            C.prototype = $.extend(new B(), new D(), {
                name : "C"
            });


            var o = new C();

            alert((o instanceof B) && (o instanceof A) && (o instanceof C)); //is instance of A, B and C 
            alert(o instanceof D); //but is not instance of D

所以,我可以调用每个方法,属性......来自A,B,C和D.问题来了,当我想测试o是D的实例?我怎样才能克服这个问题?

So, i can call every method, property ... from A, B, C and D. Problem comes, when I want to test if o is instance of D? How can I overcome this problem?

推荐答案


使用$ .extend是好的做法

Is it good practice using $.extend

$ .extend 对单身人士很有用,但原型并不理想。

$.extend is useful for singletons but for prototypes is not ideal.

使用 Object.create (或Crockford的polyfill),您可以轻松创建这样的类。我正在使用 $ .extend 来简单地处理属性并给它们提供默认值和模块模式以保持组织良好。希望这会有所帮助:

Using Object.create (or Crockford's polyfill) you can easily create classes like this. I'm using $.extend to simply process the properties and give them default values and the module pattern to keep it well organized. Hope this helps:

// Helper that makes inheritance work using 'Object.create'
Function.prototype.inherits = function(parent) {
  this.prototype = Object.create(parent.prototype);
};

var Person = (function PersonClass() {

  var _defaults = {
    name: 'unnamed',
    age: 0
  };

  function Person(props) {
    $.extend(this, props, _defaults);
  }

  Person.prototype = {
    say: function() {
      return 'My name is '+ this.name;
    }
  };

  return Person;

}());

var Student = (function StudentClass(_super) {

  Student.inherits(_super); // inherit prototype

  var _defaults = {
    grade: 'untested'
  };

  function Student(props) {
    _super.apply(this, arguments); // call parent class
    $.extend(this, props, _defaults);
  }

  Student.prototype.say = function() {
    return 'My grade is '+ this.grade;
  };

  return Student;

}(Person));

var james = new Student({ name: 'James', grade: 5 });

console.log(james instanceof Student); // true
console.log(james instanceof Person); // true

这篇关于如何使用jQuery扩展实现多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆