When I was at school, the C # teacher said that objects have two kinds of methods, static methods (Statics) and instance methods (not static), which do not understand the meaning of static, but memorize.
Later engaged in the work of the front end, has been on the class (that is, the object, JS strictly no class definition, although it is well known, here or to repeat some, so as to avoid ambiguity) of the operation, a serious lack of overall concept, recently see Extetnd again to expand the static method and instance method, so again Baidu, only to understand , actually always useful, just do not know its professional terminology ah, haha ~
static methods, which belong to the method of a class, that is, a method that a class can call directly. is shared by all instantiated objects of the class (but cannot be invoked between instance objects), so static members occupy only one area of memory;
instance method, which belongs to the method of instantiating the object after the class, that is, the method called by the instance object. Each instance of a class is created, and a piece of storage is allocated in memory for non-static members;
Static methods are instantiated at startup, so static memory is contiguous and static memory is limited, while non-static methods generate memory in the program's operation, and the application is discrete space.
Look at the code:
function A () {
}
A.staticmethof = function () {
alert (' static method ');
}
A.prototype.instacemethod = function () {
alert (' instance method ');
}
A.staticmethof (); Class A directly invokes
var instace = new A ();
With the jquery framework, the method is an instance method, and its tool functions are static methods. static method $.each (); Instance method $ (' body '). each ();
It's a good idea to talk about this.
let's look at the use of extend in jquery in two ways .
In fact, saw various frameworks and others code used to $.extend and $.fn.extend I was quite unhappy, haha, because I do not understand ... Now talk about static methods and instance methods, smart friends should be able to guess, $.extend is to expand the static method, and $.fn.extend is to expand the instance method, haha, smart ~
Say extend first.
Extend, this function is basically the function of the implementation of the object copy function, the object of all attributes are copied to another object, the development of plug-ins often used.
Look at the code:
Jquery.extend (object)
To add a method to the jquery class, add a static method:
Jquery.extend ({
min:function (A, b) {return a < B. a:b},
max:function (A, b) {return a > B? A: b }
});
Jquery.min (,);
Jquery.max (,);//
OBJECTJ query.extend (Target, object, [objectn]);
Add static methods to other classes (extend an object with one or more objects, returning the extended object
var settings = {Validate:false, limit:, Name: "foo"};
var options = {validate:true, name: "Bar"};
Result: Settings = = {Validate:true, limit:5, Name: "Bar"}
Jquery.fn
Jquery.fn = Jquery.prototype = {
Init:function (selector, context) {///...
Originally Jquery.fn = Jquery.prototype, to the prototype prototype chain is not unfamiliar?
JQuery.fn.extend (object);
Expand the Jquery.prototype to add an instance function.
For example, to develop a plugin, the contents of the Alert edit box when the edit box is clicked.
$.fn.extend ({
alertwhileclick:function () {
$ (). Click (function () {
alert ($ (this). val ())}
};
});
You can extend an object to the prototype of jquery, so that's the plug-in mechanism.
<span style= "FONT-SIZE:PX;" > (function ($) {
$.fn.tooltip = function (options) {
};
Equivalent to
var tooltip = {
function (options) {
}
};
$.fn.extend (tooltip) = $.prototype.extend (tooltip) = $.fn.tooltip
}) (JQuery);
The above is a small set of JS class to introduce static methods and examples of methods to differentiate and jquery expansion of two methods, I hope to help!