In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Baidu js's prototype article, let's take a look at W3School's introduction to prototype:
Do you think this concept is suitable for defining js's prototype? Do you also think that prototype is a property of an object object? If so, please read my article carefully, because this article will destroy your three values of life, hehe, it is so serious, because I am miserable by this definition.
I have to say, after reading some articles about prototype on the Internet, they basically say that prototype is an attribute of an object, so I firmly believe that prototype is an attribute of an object, so I have been cheated for a long time, and the consequence is that I misunderstand the js code written by others that contains prototype again and again, that is, when others show the prototype attribute of js to write js code. I look at the code they write is confused, sad ah! So, I hate prototype, so, here, I have to make the prototype attribute of js clear today. Officer, please open your eyes and take a closer look at my experiment below.
Of course, I hope you can calm down and do the following experiment again to prove that my conclusion is correct.
At the same time, in order to prove that I did not lie to you, ha ha, no more nonsense, let's enter the experimental stage.
Let's first introduce a function to be used below, JSON.stringify (value).
The function of this function is to change the incoming parameter value into a string, which has three parameters, the first parameter is required, and the other two parameters can be filled in or not.
See this article about the role of the JSON.stringify function. Http://www.cnblogs.com/ningvsban/p/3660654.html, it's very clear here.
First, test whether the definition of W3Schol works or not:
If, as W3Schol says, prototype is an attribute of object, let's create an object and see what the prototype of that object is.
Var ob = {}; / / Super simple empty object alert (JSON.stringify (ob.prototype))
What do you think will come out of the above code alert? Since prototype is an attribute of object, there must be something to get from it, right? However, if you use this code to do an experiment, you will be hit in the face, because it alert things is undefined, that is to say, object this property prototype is not a thing, very cruel, but the reality is this, any object reference prototype will appear undefined, do not believe it, you can try.
I can tell you very clearly that prototype is definitely not for an object. There is no way for an object to refer to the attribute prototype. Its real owner is actually a function. Remember, what can reference prototype is definitely a function, prototype is an attribute of a function, and prototype is an attribute of a function. Prototype is an attribute of a function, the only one that can reference it is the function, the only one that can reference it is the function, the only one that can reference it is the function, the important thing must be said thousands of times, hehe, because only by being clear about this, below can you understand what I want to talk about. Don't blame me for the ink!
Next, I'm going to give prototype a veritable definition:
Prototype is an attribute of a function and a prototype object of the function.
It's that simple. Can you read it? Prototype can only be called by functions.
Don't confuse js's object object and function function. Js's object and function are definitely two concepts. Why? Because the function function of js can new object objects, right? You should know that, right?
Next, let's do some experiments to prove my conclusion.
/ / first define a named function funcfunction func () {} alert (func.prototype)
What do you think the above code will alert? Could it still be undefined's? I am sure to tell you that it is definitely not undefined, because I have already alert, hehe.
Above, the pop-up window is:
Yes, the object is returned. Will you believe me this time that it must be a function that can call prototype? When object refers to prototype, it returns something that is not something undefined, and when a function references prototype, it returns a real thing object to you. Isn't that enough to prove that prototype is for the function? Isn't it possible to prove that the object cannot reference prototype? Ha ha, said more.
As I have said above, prototype is a property of the function and the prototype object of the function, and here the func function returns an object object when it references prototype, so what conclusion can you draw from combining these two concepts? I think it is not difficult to draw a conclusion from this:
Is it understandable that prototype is the prototype object of a function? I don't understand, it doesn't matter, let's do another experiment, and finally we're going to use a function JSON.stringify () that we introduced to you at the beginning of the article.
Function func () {} alert (JSON.stringify (func.prototype))
Again, refer to the above function func, except that what is returned here is the return value of the JSON.stringify () function.
You read it correctly, the result of alert here is an empty object, which proves that prototype really belongs to the property of the function, and the js data type of the prototype property of the function is the object, do you understand? Why is it an empty object now? Have you ever thought about it? Why? Please think about this question for three seconds. If you can't figure it out, it doesn't matter. I'll explain it next.
Let's take a look at the experimental code:
Function func () {} func.prototype.name = 'prototype is the property of the function and is essentially the prototype object of the function'; alert (JSON.stringify (func.prototype))
What do you think will come out of the above code alert? Please look at the following:
You read it correctly, at this moment, alert finally came out, and here I assign a property name to prototype, so, when I am in the prototype of the alert function func at this time, do you see any value? Do you see the property name value of prototype? Now think again, why is the first layer of alert function func.prototype above, it is an empty object? And now it is a valuable object?
The reason is very simple, because the first time, I did not assign a name attribute to the function's property func.prototype, nor did I assign a value to the name attribute, but now I have assigned the function's attribute func.prototype to the attribute name, and the assignment is that prototype is the function's property, which is essentially the prototype object of the function, so now the alert function's attribute func.prototype has a value. Right?
Therefore, the conclusion is drawn here:
Prototype is the property of the function and is essentially the prototype object of the function.
Do not think that only objects in js have attributes, through this, we can also know that, in fact, the function of js also has attributes, and the function of js seems to have only this property, prototype, and the property of this function of js is also the prototype object of the function, are you confused? I hope you're not confused.
Why is prototype an attribute of a function? Because only functions can call prototype, and func.prototype is called in this way, is this way to call things exactly the same as object calling properties? Yes, it is precisely because the function calls prototype the same as when the object calls properties, we describe prototype as a property of the function, and this property of the function is actually an object (whether this is an object or not, which has been proved above, it will no longer be explained here), so this prototype is the property of the function, which is essentially the prototype object of the function.
Why is it emphasized that the essence of prototype is the prototype object of a function?
Let's take a look at the code proof. My code is very simple:
/ / define a function function func () {} / / assign a method get func.prototype.get=function (value) to the function's property prototype {return value;// is very simple, I will output whatever you give me}
Tell me, how to call the above get method?
Let me give you a hint. Get is a property function that belongs to the func function. 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 get? Quite simply, is it OK to instantiate the object of the func function with the keyword new? Right?
Next, an object ob1 of the func function is instantiated:
Var ob1 = new func (); / / call the get property function alert (ob1.get ('hello,prototype prototype object') with the object instantiated by func
Yes, the object ob1 instantiated with the func function can indeed call the get function. The above has already used ob1 to call the get function alert, which proves that the instance object of the func function has the property function get, right? It's so obvious, there's no need to explain it.
If you don't believe that you have to call the get function with an object that instantiates func, 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 can call the get function directly. I think it must be wrong, hehe, what is the reason, you think.
So, having said so much, I still haven't explained why I say that the essence of prototype is the prototype object of a function. Right?
Look at the following code:
Var ob2 = new func (); / / call the get property method alert with the object instantiated by func (ob2.get ('I am still the object instantiated by func'))
See, I still use the func function to instantiate an ob2 again, 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 the get property function. In that case, you say, if the property prototype is not the prototype object of the function func, why can the property function get assigned to prototype be called by all instantiated objects of func? If the essence of prototype is not the prototype object of the func function, then it is impossible for all the objects instantiated according to the func function to call the get property method, right? Why? Let's take a proportion, you say, something that is not the source, can it affect everything? You can't, can you? Therefore, the conclusion is drawn here:
Prototype is an attribute of a function, which is essentially the prototype object of the function.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.