In JavaScript, each function object has a default property of prototype, called the prototype member of the function object, which points to an object, called the prototype object of the function, and when each of us defines a function, JavaScript creates a corresponding prototype object. In other words, when we define a function, we actually get two objects, a function object, a prototype object. The prototype object is a special object, and the prototype member of the function points to its prototype object.
A reference to this prototype object can be obtained through the prototype member of the function object.
The following defines a function object person and then obtains its prototype object through prototype. A method is then defined on its prototype object.
01
function person ()
02
{
03
}
04
05
Person.prototype.showPerson = function ()
06
{
07
Alert ("Person Object.");
08
}
09
10
var alice = new Person ();
11
Alice.showperson ();
The members defined on this prototype object will be used for sharing with all objects created through this function. Equivalent to instance methods in C #, objects, functions, and prototypes in-memory relationships as shown:
Each object also has a prototype member prototype, the object created by the new function finds the prototype of the function through the prototype of the function, and then points its prototype to the object. For object instances and prototype objects that are not created by functions, their prototypes are set to the prototype object of the object function.
The object function objects are top-level function objects defined in JavaScript, and all objects in JavaScript use the prototype of the object directly or indirectly. When accessing an object's properties or methods, if the object itself does not have this property or method, then JavaScript checks whether the object's prototype object has this property or method, and if so, returns it as the object's property or method, if not, through the prototype object's Prototype continues the check until the prototype object is a prototype object of the object function.
But prototype is a special attribute, and in most browsers, such as IE, you cannot directly access the prototype members of an object. The returned result is undefined. You cannot give an object a new prototype, only by creating its function to determine the object's prototype.
The prototype of a function object has a special purpose, that is, the object created by the function new will automatically give the prototype of the function object to the newly created object. Thus, if a prototype object is set for a function, all objects created by this function will have the same prototype object. In this way, you can make these objects share the same properties or methods to emulate the concept of a type.
In JQuery, the $ () function that we often use is the $ () function that is defined on the Window object.
1
JQuery = Window.jquery = window.$ = function (selector, context)
2
{
3
The JQuery object is actually just the Init constructor ' enhanced '
4
return new JQuery.fn.init (selector, context);
5
}
This function is actually done through the new JQuery.fn.init (selector, context), which is the creation of an object through the INIT function.
The prototype object for function init is re-specified below. So the prototype object of the INIT function is the FN object.
1
Give the init function The JQuery prototype for later instantiation
2
JQuery.fn.init.prototype = Jquery.fn;
All objects created through $ will share the members on the FN object. As a result, jQuery objects have methods like attr, HTML, and so on.
It is easier to understand the prototype diagram of JQuery by tidying it up.
Legend: Yellow is the object, and blue is the function.
Analysis of prototype prototype in jquery