Another old friend after typeof!
Prototype is also an old friend of ours, and even those who do not know it should have heard its name. If it's still your new friend, I guess you're also a new friend of JavaScript.
In our first section (Understanding JavaScript Prototypes and Scope series (1)-Everything is an object), the function is also an object. He is also a collection of attributes, and you can also customize the properties for the function.
Instead of waiting for us to experiment, JavaScript does its own first example, and the default is to give the function an attribute--prototype. Yes, each function has a property called prototype.
The attribute value of this prototype is an object (a collection of attributes, again emphasized!). ), the default is only a property called constructor, pointing to the function itself.
For example, Supertype is a function, and the right-hand box is its prototype.
Since the prototype as an object, the collection of properties, it is impossible to just get a constructor to play, certainly can be customized to add many properties. For example, this object elder brother, People's prototype inside, there are several other attributes.
Hey, how do some of these methods seem to have been similar?
Right! Don't worry, then let you know why they are familiar.
Next, you can also add your own properties to the prototype of your own custom method.
function Fn () {} = ' Wang Fu '; function () { return 1988; };
See no, this becomes the
No problem!
But what is the use of this? To solve this problem, let's talk about jquery first.
var $div = $ (' div '); $div. attr (' myName ', ' Wang Fu ');
In the above code, $ (' div ') returns an object, Object--created by the function. Assume that the function that created this object is myjquery. It's actually done this way.
function () { //... }; $ (new myjquery ();
I wonder if you can see it.
If we use our own code to demonstrate it, that's it.
function Fn () {} = ' Wang Fu '; function () { return 1988; }; var New Fn (); Console.log (fn.name); Console.log (Fn.getyear ());
That is, FN is a function, and the FN object is from the FN function new, so that the FN object can invoke the properties in Fn.prototype.
Because each object has a hidden property--"__proto__", this property references the prototype of the function that created the object. That is: fn.__proto__ = = = Fn.prototype
Here the "__proto__" becomes the "implicit prototype", and the next one continues to decompose.
---------------------------------------------------------------------------
This article has been updated to the directory of Understanding JavaScript prototypes and scope series, and more on understanding JavaScript prototypes and Scope series.
Also, please pay attention to my Weibo.
Also welcome to follow my other tutorials-don't be afraid, are free!
"Microsoft petshop4.0 Source Interpretation Video" "Json2.js Source Interpretation video"
--------------------------------------------------------------------------
Understanding JavaScript Prototypes and Scope series (3)--prototype prototypes