The day before yesterday, a netizen left a message asking me, what is Object? Since I have been busy reconstructing the TreeView control for the past two days, I am really sorry that I did not respond in time. Take some time today to take a look at what exactly is Object in JavaScript? What is the relationship between Object and Function? I welcome corrections if I am wrong.
Although it is a private message, I hope you will not blame me if you send it anonymously. If there is anything wrong, please let me know.
What exactly is the Object of js?
At first I thought Object was the prototype of all objects in js.
But: alert(Object.constructor) displays function Function...
This means that the prototype of Object is Function?
But the problem comes again:
Function.prototype.read=function(){};//Extended Function prototype
for(var i in Object)alert(i)//displays read, which further confirms that the prototype of Object is Function
Object.prototype.read=function(){};//extends the prototype of Object
for(var i in Function)alert(i)//Display read, the prototype of Function is Object? ? ? ?
What exactly is Object? Are Object and Function as classes the same thing?
This friend confused Constructor, Prototype and Function because JavaScript is an Object-based language (JavaScript does not contain proper classes). It is actually possible to say that Object is the prototype of all objects, but this refers to the concept of prototype in the Prototype Pattern in the design pattern, not the prototype language feature of JavaScript called Object.prototype.
So what exactly is Object in JavaScript? Script56.chm (the official M$ tutorial) says: Provides common functions for all JScript objects. Well, do you understand? Because I should understand, but I still don’t seem to understand @_@. If we look at the data structure, an object (an instance of Object) is an unordered collection, a structure similar to map in C++, hashtable in C#, and hashmap in Java. And it contains a primitive value assigned by the JavaScript language system. What does it mean? Object has a method called valueOf, its function is to return the original value of the specified object. This can also be found in Script56, and there is also a table listing the valueOf return results of system objects. In other words, objects such as Array, Boolean, Date, Function, Number, etc. actually all come from Object, and their ancestors are all Object. They represent different language features. For example, Array has an automatically managed length attribute, Boolean only has true or false values, Date represents a time structure, and Function can be run. These are all capabilities given to them by their original type (valueOf). Object is actually just a concept. The JavaScript language is based on objects, which means that all built-in types are abstracted from a set of common methods and properties (also called behaviors and states). Let us imagine that one thing that only has these characteristics is Object. In fact, Object is not very useful in programming. We are all using the instance object of Object, and then using the collection feature of Object (expando) to expand the object to become what we want. For Object.prototype, it is actually not very useful, because each specific type has its own prototype, and most of the prototype methods we add are for certain types.
In addition to prototype, Object also has a very important attribute - constructor. This thing is used to complete the expansion of object I mentioned earlier. It is also the basis for us to use JavaScript to simulate OOP. Since everything in JavaScript is Object, so is the constructor, but its original type is Function (run Object.constructor.valueOf() to get: function Function() { [native code] }). Of course, conversely, not all JavaScript objects have a constructor attribute, and some built-in objects do not have a constructor.
Regarding the relationship between Object and Function, I think this is not a good test code: Function.prototype.read=function(){};//Extended Function prototype
for(var i in Object)alert(i)//displays read, which further confirms that the prototype of Object is Function
Object.prototype.read=function(){};//extends the prototype of Object
for(var i in Function)alert(i)//Display read, the prototype of Function is Object?
These four lines of code are used to explain the principle of JavaScript's prototype and simulate the prototype inheritance method of OO programming. They are quite sexy! However, they cannot clearly explain the relationship between Object and Function: (On the contrary, they will fool the audience.
Let's briefly describe each of the various functions in JavaScript. object types:
Native Object: Objects provided by the JavaScript language that do not depend on the execution host. Some of them are built-in objects, such as Global and Math; some are created and used in the script running environment, such as: Array, Boolean, Date, Function, Number, Object, RegExp, Error.
Build-in Object: Built-in objects provided by the JavaScript language that do not depend on the execution host, such as Global and Math; built-in objects are all Native Objects.
Host Object: Any object provided by the JavaScript language that depends on the host environment. All non-Native Object objects are host objects, such as: window in IE, wscript instance in WScript, any user-created class