Simple comprehension of JS's prototype property

Source: Internet
Author: User

Before entering the text, I have to first say that I know JS prototype this thing of the tortuous process.

Baidu JS prototype article, first look, W3school about prototype introduction:

  

Do you think this concept is suitable for defining JS prototype this thing? Do you also think that prototype is a property of object objects? Yes, please seriously read my article, because this article will destroy your life three views, hehe, is so serious, because I was this definition to harm miserably.

Have to say, read some of the online introduction prototype article, basically said prototype is a property of the object, so I also firmly believe that prototype is an object property, so, I was a long time, so, the result is that, I have misunderstood the other people wrote the JS code containing prototype, that is, when others flashed JS prototype this attribute to write JS code, I look at their code is to write  Confused , sad and hurried ah! So, I hate prototype this thing, so, here, I have to put JS prototype attribute Tao plainly. Crossing, please polish your eyes and look carefully at the experiments below me.

Of course, I hope that all of you crossing can also calm down and put my next experiment again, to prove my conclusion is correct.

At the same time, also in order to prove I don't have it. Deceive you, hehe, nonsense not much to say, the following into the experimental phase.

Let's start by introducing a function below, json.stringify (value).

The function is: the passed parameter value into a string, it has three parameters, the first parameter is necessary, the remaining two parameters can be filled.

See this article for the role of the json.stringify function. Http://www.cnblogs.com/ningvsban/p/3660654.html, that's very clear.

First, test the definition of W3schol to the end of the line:

If, as W3schol says, prototype is a property of object, then let's create an object to see what the prototype of this object really is.

var ob = {}; // super Simple empty Object alert (json.stringify (Ob.prototype));

What do you think the code above will alert? Since prototype is a property of object, then it is certain to get something on it, right? But if you take this piece of code to do the experiment, you'll be punched in the face because it's alert something is Undefined, that is, object this property prototype is not a thing, very cruel, but the reality is so, any object reference prototype will appear undefined, do not believe, you can try.

I can clearly tell you, prototype absolutely not for the object, the object has no way to reference prototype this attribute, it is the real owner, is actually function, remember, the ability to reference prototype is definitely a function, absolutely a function, absolutely a function, prototype is a property of the function, prototype is a property of the function, prototype is a property of the function, The only that can reference it function, the only that can reference it function, can refer to it only Functions, functions, functions, functions, important things must be said times, hehe, because only clear this point, below, you just understand what I want to talk about. Don't blame me for the ink!

Do not believe, you take the object to quote it AH? To see if it is undefined for the newspaper, if, each object reference prototype undefined, you also think the object can reference prototype? Do you still think that prototype is a property of the object?

If, even if the object refers to prototype newspaper undefined can not convince you prototype not belong to the object, then, I think you do not have to look down, because of the nature of prototype misunderstanding, you are very sick, no medicine can be saved. That's all I can say.

Below, I want to give prototype a veritable definition, please open your eyes to see AH hehe.

    Prototype is a property of a function and is the prototype object of a function.

It's so simple, can you see? prototype can only be called by a function.

Don't confuse it. js object and Function function, JS objects and functions are absolutely two concepts, why? Because JS's function functions can be new to object objects, right? You should know this, right?

Below, to do the experiment to prove my conclusion.

// first define a well-known function func function func () {    }alert (func.prototype);

You say, what does the code above alert? Or will it be undefined? I'm sure to tell you definitely not undefined, because I have alert over, hehe.

Above, the popup window is:

Yes, the return is the object, this time you will believe me, can call prototype is a function of it? Object refers to prototype when you return a thing is not something undefined, function reference prototype when you return a real real thing object, it is not enough to prove that prototype is for the function? Is it not possible to prove that the object cannot refer to prototype? Oh, and more said.

As I have said above, prototype is a property of a function and a prototype object of a function, and here the Func function refers to prototype when it returns an object, so what conclusion can you draw from the combination of these two concepts? I think it's easy to conclude by this:

Prototype is. The prototype object of the function, does that make sense? Do not understand, no matter, we will do an experiment, finally to use the article began to introduce you to a function of json.stringify ().

function func () {    }alert (json.stringify (Func.prototype));

or refer to the function func above, except that the return value of the json.stringify () function is returned here.

You are not mistaken, the result of alert here is an empty object, which proves that prototype really belongs to the function of the property, and the function of the prototype property of the JS data type is an object, understand? Why is it now an empty object? Have you ever thought about it? Why is it? Please think about this question for three seconds, if you can't figure it out, then it doesn't matter, below, I'll explain.

Look first, the experiment Code:

function func () {    = ' prototype is the property of a function, essentially a prototype object of a function '; alert (json.stringify (func.prototype))

You say, what does the code above alert out? Please see below:

You are not mistaken, at the moment, finally alert out of the thing, I am here to give prototype a property name, so, I this time in the Alert function func prototype, you see a value? Have you seen the property name value for prototype? Now think again, why is it an empty object when the first alert function above Func.prototype? And now it's a valued object?

The reason is very simple, because the first time, I did not give the function's property func.prototype to the Name property, and did not assign a value to the name attribute ah, and now I have to give the property Func.prototype property name, and assign a value of Prototype is the property of the function, essentially the prototype object of the function ... so, now that the alert function's property is Func.prototype, it has value, right?

So here's the conclusion:

Prototype is the property of a function, which is essentially a prototype object of a function.

Do not think that only the object JS in the attribute, through here, we can also know, in fact, JS function is also a property, and JS function as if only this attribute prototype, and JS This function attribute is also the function of the prototype object, you are not dizzy? I hope you didn't get dizzy.

Why is it that prototype is a property of a function? Because, only the function can call prototype, and in such a way func.prototype called, such a way to call things are not the same as the object invocation properties? Yes, just because the function call prototype is the same as when the object calls the property, we put prototype as a property of the function, and this property of the function is actually an object (this is not the object, which has been proved, here is no longer explained), so say, This prototype is the property of the function, which is essentially the prototype object of the function.

Why does this emphasize that the essence of prototype is the prototype object of a function?

Here's the code to prove that my code is simple:

    // define a function    function func () {        }    // gives a method to the property prototype of the function get    func.prototype.get=  function(value) {        return value;   very simple, you give me what I will output what    }

You say, how to call the above get method?

I give you a hint that get is a property function of the Func function, and since it is a property function, how do we call it?

Very simply, the property function must be called by its object, so how do we get the object of Get? Is it simple to instantiate the object of the Func function with the keyword new? Right?

Below, instantiate an object of the Func function Ob1:

var New func (); // Call the Get Property function alert (Ob1.get (' Hello,prototype prototype object ') with the object instantiated by Func ;

Yes, the object that is instantiated with the Func function Ob1, really can call get function, above already use OB1 call get function alert come out, this proves that the instance object of the Func function has get this property function, right? So obviously, you don't have to explain it.

If, you do not believe that you have to instantiate the Func object to invoke the Get function, you can try to call the Get function directly with Func, that is, you can try Func.get () call the Get function to see, see if Func it can not directly call the Get function. I would like to make an error, oh, what is the reason, you think.

So, having said so much, I still do not explain why I say the essence of prototype is the prototype object of the function? Right?

Take a look at the following code:

var New func (); // Call the Get property method with the object instantiated by Func (Ob2.get (' I am still an instantiated object of Func '));

 

See, I'm still using the Func function again to instantiate a OB2, and this object can still invoke the Get property function, which means what? This shows that all objects instantiated with the Func object can call the Get property function. In that case, you say, prototype, if this property is not a prototype object of function Func, then why is the property function get given to prototype to be called by the Func all instantiated object? If the essence of prototype is not the prototype object of the Func function, then the object that is instantiated from the Func function is not capable of invoking the Get property method, right? Why? We have a ratio, you say, a not source of things, it can affect all things? Can't you do that? So here's the conclusion:

Prototype is a property of a function, which is essentially a prototype object of a function.

The whole article is to illustrate this conclusion. Without him. I hope crossing can read this article. For the application of prototype next article, stop here.

Simple comprehension of JS's prototype property

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.