Simple understanding of JS prototype properties and the use of _javascript techniques

Source: Internet
Author: User

Before entering the main body, I must first say that I know the prototype of JS this thing twists the process.

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

Do you think this concept is suitable for the definition of JS prototype this thing? Do you also think that prototype is a property of object objects? Yes, please seriously look at this article, because this article will destroy your life three views, oh, is so serious, because I was this definition to harm miserable.

Have to say, read some of the online introduction prototype article, basically said prototype is an object of an attribute, so, I also firmly believe that prototype is an object of the attributes, so, I was a long time in the pit, thus, the consequences are, I misunderstood the other people wrote a prototype of JS code, that is, when others flashed JS prototype this attribute to write JS code, I watched them write the code is  Confused , sad urge Ah! So, I hate prototype this thing, so, here, I must put the JS prototype attribute road a clear. Reader, please polish your eyes and look carefully at my following experiment.

Of course, I hope that you reader also be able to calm down and put my next experiment again, so as to prove my conclusion is correct.

At the same time, in order to prove I don't have it. Cheat you, hehe, the nonsense does not say much, below enters the experiment stage.

Let's introduce a function to be used below, json.stringify (value).

The function is to change the parameter value passed into a string, it has three parameters, the first parameter is required, and the remaining two parameters can be filled in.

On the function of json.stringify please read this article. Http://www.jb51.net/article/29893.htm, it's very clear here.

First of all, test the definition of W3schol to the end of the pass:

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

var ob = {};//super simple null object
alert (json.stringify (Ob.prototype));

What do you think the code above will alert you to? Since prototype is an attribute of object, it must be able to get something on it, right? But if you're experimenting with this code, you're going to get hit in the face because it's alert. Undefined, that is, object this property prototype is not a thing, very cruel, but the reality is so, any object quoted prototype will appear undefined, do not believe, you can try.

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

Next, I want to give prototype a veritable definition:

Prototype is a property of a function that is a prototype object of a function.

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

Don't get mixed up. JS object and function functions, JS objects and functions are absolutely two concepts, why? Because the function functions of JS can be new object objects Ah, right? You should know that, right?

Below, to do the experiment to prove my conclusion.

First define a well-known function Func function
func () {
  
}
alert (Func.prototype);

What do you think the code above will alert you to? Will it be undefined? I am sure that you are not undefined, because I have already alert, hehe.

Above, the pop-up window is:

Yes, return is the object, this time you will believe me, can call prototype must be a function of it? Object refers to prototype when you return is something that is not something undefined, the function refers to the prototype when you return a true real existence of object, this is not enough to prove that prototype is for the function? Can not prove that the object can not be quoted prototype? Oh, and more said.

As I have said above, prototype is a function of a property, is also the prototype object of the function, and here Func function refers to the prototype when the return of an object objects, then, combining these two concepts, you can draw what conclusion? I think it's not hard to draw the conclusion that

Prototype is. of the prototype object of the function, can you understand it? Can not understand, there is no relationship, we have to do an experiment, and finally to use the article began to introduce you to a function 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're right, here. The result of alert is an empty object, which proves that prototype really belongs to the properties of the function, and that the JS data type of the function's prototype attribute is an object, do you understand? Why is it an empty object now? Have you ever thought about it? Why, then? Please think about this question for three seconds, if you can't think of it, then it doesn't matter, let me explain.

First Look, the experimental code:

function func () {
  
}
Func.prototype.name = ' prototype is the property of a function, which is essentially a prototype object of a function ';
Alert (Json.stringify (Func.prototype))

What do you think the code above will alert you to? Please look below:

You're not mistaken, at the moment, and finally alert is coming out, and I'm here to give prototype a property name, so, when I'm at this point in the alert function func prototype, do 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-level alert function is Func.prototype? And now it's a valued object?

The reason is very simple, because the first time, I did not give the function Func.prototype attribute to the Name property, nor to the Name property assignment Ah, and now I have given the function Func.prototype attribute name, and assign the value of Prototype is a function of the property, essentially the function of the prototype object, so, now the alert function of the property func.prototype when it has a 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 JS only objects have attributes, through here, we can also know, in fact, the function of JS is also a property, and JS functions as if only this attribute prototype, and JS This function property is also a 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 a function can invoke prototype, and it is called in such a way that the invocation of something is not exactly the same as the object invocation property Func.prototype? Yes, that's because the function call prototype is the same as when the object calls the property, and we call prototype a function of the property, and the function of this property is actually an object (this is not the object, the above has been proved, here is no longer explained), so say, This prototype is the property of the function, which is essentially a prototype object of the function.

Why do we emphasize that the essence of prototype is the archetypal object of function?

The code below shows that my code is simple:

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

You say, how do you call the above get method?

Let me give you a hint that get is a property function that belongs to the Func function, and since it is an attribute function, how do we call it?

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

Below, instantiate an object of the Func function Ob1:

var ob1 = new Func ();
An object instantiated with Func invokes the Get Property function
alert (ob1.get (' Hello,prototype prototype object '));

Yes, the object ob1 with the Func function can actually call the Get function, which already uses OB1 to call get function alert, which proves that the instance object of the Func function has the property function of Get, right? So obviously, you don't have to explain it.

If you don't believe you have to call the Get function with an instantiated Func object, you can try calling the Get function directly with Func, that is, you can try Func.get () to call the Get function to see if func it can directly call getting functions. I want to make a certain error, hehe, the reason is what you think.

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

Take a look at the following code:

 var ob2 = new Func ();
An object that is instantiated with Func invokes the Get property method
alert (Ob2.get (' I'm still a Func instantiated object '));

See, I'm going to use the Func function again to instantiate a OB2, and this object can still call the Get property function, what does that mean? This shows that all objects instantiated with the Func object can call get this property function. In this case, you say, prototype this attribute is not a prototype object of function Func, then why is the property function get given by prototype to be invoked by Func all instantiated objects? If the essence of prototype is not a prototype object of the Func function, then an object instantiated from the Func function cannot call get this property method, can it? Why? We have a ratio, you say, a source is not a thing, it can affect all things? Can you? So here's the conclusion:

Prototype is an attribute of a function, which is essentially a prototype object of a function.

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

Special Note:

    • Array.prototype is an array
    • String.prototype is a string
    • Object.prototype is an object

These three special examples, unlike the prototype of constructors, of course, if you really understand that prototype is a prototype object of a function, you should know that the prototype object of an array should be an array, not an object? The prototype object of a string should be a string and not an object? Please see the next article for details.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.