jQuery - 如何编写类来实现OO设计

Rob*_*bin 1 javascript jquery

我在过去使用过Prototype.js并且能够编写类uing:

var XEventDesc = Class.create();

XEventDesc.prototype = {

    initialize: function(element, eventName, handler, useCapture) {
        ....................
    }
};
Run Code Online (Sandbox Code Playgroud)

如何使用jQuery在Javascript中编写类

Ran*_*Dev 7

你真的需要使用jQuery来创建一个类吗?javascript对象只是一个函数.

var Rectangle = function(width,height) {
    //This section is similar to the initialize() method from prototypejs.
    this.width = width;
    this.height= height;

    //Adding a method to an object
    this.getArea = function () {
        return this.width*this.height;
    }
}
var myRect = new Rectangle(3,4);
alert(myRect.getArea()); //Alerts 12
Run Code Online (Sandbox Code Playgroud)