Object oriented
Several important features of object-oriented thinking (requirements for classes):
Abstraction-encapsulation, information hiding (internally implemented methods and data hiding, defining open interfaces)
Inheritance-subclasses can use the resources of the parent class, and can customize their own resources, including methods and data
Polymorphic-Overloaded (function with the same name), overwrite (overriding parent-class function on the basis of inheritance)
JS and Object-oriented
JavaScript uses prototype to implement class inheritance, non-classic object-oriented language classes, and the use of different methods, resulting in more difficult to use.
Please refer to the master's "in-depth understanding of JavaScript prototypes and closures series" http://www.cnblogs.com/wangfupeng1988/p/4001284.html
So various libraries provide their own implementation of the class library, for example:
1. jquery Class Create:https://github.com/protonet/jquery-class-create
Use Class.create to create a utility class, the constructor of an instance, with the disadvantage of using the jquery library
<pre>
// properties are directly passed to `create` method var Person = Class.create({ initialize: function(name) { this.name = name; }, say: function(message) { return this.name + ‘: ‘ + message; } });
// when subclassing, specify the class you want to inherit from var Pirate = Class.create(Person, { // redefine the speak method say: function($super, message) { return $super(message) + ‘, yarr!‘; } });
var john = new Pirate(‘Long John‘); john.say(‘ahoy matey‘); // -> "Long John: ahoy matey, yarr!"
</pre>
2, Prototypejs:http://prototypejs.org/learn/class-inheritance
As a function of the Prototypejs library, the use is consistent with the jquery class create interface.
This library also provides an elegant interface for Ajax and DOM, eliminating the complexity of client development.
These two libraries either rely on other libraries, or their own library function complex, volume is large, so the need to use alone is not satisfied.
This article describes a library that does not rely on any library, and implements only the class inheritance, a standalone library developed by the Great God: John resig http://ejohn.org/
Simple JavaScript Inheritance
Simple JavaScript Inheritance
This library's official website is
http://ejohn.org/blog/simple-javascript-inheritance/
The goal of the author is simple-easy to understand, reusable-not dependent on other libraries, using examples:
<pre>
var person = class.extend ({
Init:function (isdancing) {
this.dancing = isdancing;
},
Dance:function () {
return this.dancing;
}
});
var Ninja = Person.extend ({
Init:function () {
This._super (FALSE);
},
Dance:function () {
Call the inherited version of Dance ()
return This._super ();
},
Swingsword:function () {
return true;
}
});
var p = new Person (true);
P.dance (); = True
var n = new Ninja ();
N.dance (); = False
N.swingsword (); = True
Should all is true
P instanceof person && p instanceof Class &&
n instanceof Ninja && n instanceof person && n instanceof Class
</pre>
Implementation notes:
1. Creating a constructor must be simple, and the constructor provides only the Init initialization method.
2. To create a new class, you must extend an existing class to invoke extend.
3. All inheritance with the only ancestor Class. The new class created must be a subclass of class.
4. A method with the same name that accesses the parent class in the subclass (that is, overwritten) must be provided. A method with the same name as the parent class is called through the This.super child class with the same name.
Implementation Essentials
Implementation code:
<pre>
/* Simple JavaScript inheritance
* by John Resig http://ejohn.org/
* MIT Licensed.
*/
Inspired by Base2 and Prototype
(function () {
var initializing = false, Fntest =/xyz/.test (function () {xyz;})? /\b_super\b/:/.*/;
The base Class implementation (does nothing)
This. Class = function () {};
Create a new class that inherits from this class
Class.extend = function (prop) {
var _super = This.prototype;
Instantiate a base class (but only create the instance,
Don ' t run the Init constructor)
initializing = true;
var prototype = new This ();
initializing = false;
Copy the properties over onto the new prototype
for (var name in prop) {
Check if we ' re overwriting an existing function
Prototype[name] = typeof Prop[name] = = "function" &&
typeof _super[name] = = "function" && fntest.test (Prop[name])?
(function (name, FN) {
return function () {
var tmp = This._super;
Add a new _super () method is the same method
But on the Super-class
This._super = _super[name];
The method is need to being bound temporarily, so we
Remove it when we ' re done executing
var ret = fn.apply (this, arguments);
This._super = tmp;
return ret;
};
}) (name, Prop[name]):
Prop[name];
}
The Dummy class constructor
function Class () {
All construction is actually do in the Init method
if (!initializing && this.init)
This.init.apply (this, arguments);
}
Populate our constructed prototype object
Class.prototype = prototype;
Enforce the constructor to being what we expect
Class.prototype.constructor = Class;
And make this class extendable
Class.extend = Arguments.callee;
return Class;
};
})();
</pre>
Points:
1. Initialize init in call Xx.extend{init:function () {}}
The Dummy class constructor
function Class () {
All construction is actually do in the Init method
if (!initializing && this.init)
This.init.apply (this, arguments);
}
2. Subclasses access the parent class's function with the same name through This._super, for example:
var person = class.extend ({
Init:function (isdancing) {
this.dancing = isdancing;
}
});
var Ninja = Person.extend ({
Init:function () {
This._super (FALSE);
}
});
var p = new Person (true);
p.dancing; = True
var n = new Ninja ();
n.dancing; = False
As follows, the son has this function, and the father also has this function of the same name, when the function address is recorded in This._super.
Check if we ' re overwriting an existing function
Prototype[name] = typeof Prop[name] = = "function" &&
typeof _super[name] = = "function" && fntest.test (Prop[name])?
(function (name, FN) {
return function () {
var tmp = This._super;
Add a new _super () method is the same method
But on the Super-class
This._super = _super[name];
The method is need to being bound temporarily, so we
Remove it when we ' re done executing
var ret = fn.apply (this, arguments);
This._super = tmp;
return ret;
};
}) (name, Prop[name]):
Prop[name];
}
Simple JavaScript inheritance--a minimalist JS inheritance library