About prototype and JSprototype in JS
What is prototype:
The function-defined object has a prototype attribute, which points to another prototype object. Note that the prototype attribute is different from the prototype object. Another constructor attribute exists in the prototype object. This constructor attribute also points to a constructor object, and this constructor object is exactly the function itself.
// Determine whether it is an Array function isArray (obj) {return Object. prototype. toString. call (obj) = '[object Array]';}; // determines whether it is functionfunction isFunc (obj) {return Object. prototype. toString. call (obj) = '[object Function]';} // whether it is a json object function isJson (obj) {return typeof (obj) = "object" & (isArray (obj) | Object. prototype. toString. call (obj ). toLowerCase () = "[object]");}
My understanding of this attribute is to inherit the methods and attributes of the prototype object. What are the attributes and methods? Some new people may not know much about it. For example, the attributes of a bicycle include red appearance, variable speed, and illumination. What is the attribute of the bicycle? The method is to ride a bicycle to go to school and so on. The way is what the object can do?
In this case, I will talk about her understanding from the meaning to the application. For the shortcomings, please give me some advice.
What does prototype mean?
Prototype attribute in javascript: return the reference of the Object Type prototype.
For example,
A.prototype = new B()
Prototype of A is an instance of B. That is to say, A clones the methods and attributes in B. Note that this is cloning rather than inheritance. As for the difference between cloning and inheritance, you can ask du Niang if you are interested.
The following is a simple example:
Html section:
<Body> <a class = "btn btn1"> button 1 </a> <a class = "btn btn2"> button 2 </a> <a class = "btn btn3 "> button 3 </a> </body>
Js section:
(Function () {var btn1 = $ (". btn1 "); var btn2 = $ (". btn2 "); var btn3 = $ (". btn3 "); function baseClass () {this. showMsg = function () {alert ('000000') ;}} function extendClass () {this. showMsg = function () {alert ('000000') ;}// the prototype attribute returns a reference to the prototype of the object type. // If the constructor and the prototype function have a method of the same name, then, the constructor method is preferentially searched, and the same name function extendClass of the prototype function is not cloned again. prototype = new baseClass (); var initance = new extendClass (); btn1.click (function () {initance. showMsg () ;}); var baseinitance = new baseClass (); btn2.click (function () {baseinitance. showMsg. call (initance );})})();
To understand the above examples, you can have a rough understanding of prototype attributes in js.
The above is the prototype in JS introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!