Home > Web Front-end > JS Tutorial > body text

Three simple pieces of code to verify the relationship between Object and Function in javascript_javascript skills

WBOY
Release: 2016-05-16 18:24:18
Original
1063 people have browsed it

It is said that when Louzhu's understanding and practical ability were not yet mature, in this article, he copied a lot of things from Teacher Li Zhan and wrote them in his blog as a "knowledge reserve". This time it is still inevitable. When I turned to Chapter 5, I was deeply attracted and impressed by the second paragraph of the chapter: "Functions have all the characteristics of objects. You can call functions as objects. In fact, functions are objects, but they are different than ordinary objects. There is an additional bracket "{}" operator, which is used to execute the logic of the function, that is, the function itself can be called, but general objects cannot be called, except that it is exactly the same." A few words, but a profound explanation of the relationship between objects and functions. Below, Lou Zhu will demonstrate the relationship between JavaScript's built-in Object and Function through a few simple pieces of code he wrote.
 
1. Function is Object, Object is Function

Copy code The code is as follows:

alert(Function instanceof Object); // true
alert(Object instanceof Function); // true

As you can see, by Instanceof operator, the function is the object, and the object is the function.
2. Since 1 is true, can Object "get" the prototype method extended by Function?
Copy code The code is as follows:

alert(Object.funcMethod); // undefined
Function.prototype.funcMethod = function() {
/*some function method code here*/
}
alert(Function.funcMethod);
alert(Object.funcMethod);
alert(Function.funcMethod === Object.funcMethod); //true

You read that right, the prototype method funcMethod we extended for Function, Object has achieved the magical "get something for nothing" .
3. Since 1 and 2 are both true, can Function "get" the prototype method of Object extension? !
Code
Copy code The code is as follows:

alert(Function.objMethod ); // undefined
Object.prototype.objMethod = function() {
/*some object method code here*/
}
alert(Object.objMethod);
alert(Function .objMethod);
alert(Function.objMethod === Object.objMethod); //true or false?

Does the line with a question mark at the end of the above code pop up true or false? Woolen cloth? To put it bluntly, based on Lou Zhu’s straightforward and simple statements throughout the article, you should already know the result, and I won’t announce the answer here.
Finally, Rong Louzhu is proud to be narcissistic here: I personally think that the above three pieces of code should be more convincing than the code in the original book that verifies "functions are the essence of objects".
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template