What is the object in JavaScript ?!

Source: Internet
Author: User
Some netizens left me a message the day before yesterday and asked me what the object is? I am so sorry because I have been busy refactoring the Treeview control in the past two days and have not answered it in time. Today, let's take a look at what the object in Javascript is? What is the relationship between objects and function functions? If you say something wrong, you are welcome to make an axe.

Although it is a private message, I hope it will not blame me for turning it over anonymously. Please let me know if there is anything wrong.What is JS object?
At first, I thought that the object was the prototype of all JS objects.
However, alert (object. constructor) displays the function...
This indicates that the prototype of the object is function?
But the problem comes again:
Function. Prototype. Read = function () {}; // prototype of the Extended Function
For (var I in object) Alert (I) // display read, which further confirms that the object's prototype is Function

Object. Prototype. Read = function () {}; // extends the object prototype.
For (var I in function) Alert (I) // display read. The function prototype is object ????
What is an object? What is the same thing between an object and a function as a class?

This friend confused constructor, prototype, and function because Javascript is an object-based language (JavaScript does not contain proper classes ). Objects are the prototype of all objects, but they are the prototype concept in the design pattern, rather than the prototype language feature of the JavaScript Object. prototype.

so what exactly is the object in JavaScript? Script56.chm (the official M $ tutorial): provides common functions for all JScript objects. Well, do you understand? Because it should be clear, but the friendly image still does not understand @_@. In terms of data structure, an Object is an unordered set, similar to a structure such as map in C ++, hashtable in C #, and hashmap in Java. It also contains the original value assigned by a javascript language system. What does it mean? An object method is valueof. Its function is to return the original value of the specified object. This can also be found in script56, and another table lists the valueof returned results of the system object. That is to say, objects such as array, Boolean, date, function, and number come from objects, and their origins are all objects. They represent different language features. For example, array has automatically managed length attributes. boolean only has true or false values, date indicates the time structure, and function can be run, are the capabilities granted to them by their primitive type (valueof. Object is actually just a concept. The javascript language is based on objects. It means that all built-in types are abstracted out a set of common methods and attributes (also called behavior and status ), we can imagine that only one thing with these features is object. In fact, the object is not very useful in programming. We are all using the object instance Object , and then using the object set feature (expando ), expanding Object is what we want. In fact, object. Prototype cannot be used. Because each exact type has its own prototype, we add many primitive methods for specific types.

In addition to prototype, an object also has a very important attribute-constructor. This is used to completeObjectIt is also the basis for us to simulate OOP using JavaScript. Since everything in Javascript is an object, so is constructor, but its original type is function (run object. constructor. valueof (): function () {[native code]}). Of course, not all JavaScript objects have the constructor attribute. Some built-in objects do not have the constructor attribute.

I think this is not a good test of the relationship between objects and functions.Code:

Function. Prototype. Read = function () {}; // prototype of the Extended Function
For (var I in object) Alert (I) // display read, which further confirms that the object's prototype is Function

Object. Prototype. Read = function () {}; // extends the object prototype.
For (var I in function) Alert (I) // display read. The function prototype is object ????

These four lines of code are used to explain the prototype principle of JavaScript and compare the original Inheritance Method of simulating OO programming with sexy! However, they cannot clearly describe the relationship between objects and functions: (rather, they will be fooled.

The following describes various object types in javascript:
native object: objects provided by the Javascript language that do not depend on the execution host, some of which are built-in objects, such: global and math are created and used in the script running environment, such as array, Boolean, date, function, number, object, Regexp, and 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. All built-in objects are native objects.
Host Object: Any objects provided by the Javascript language that depend on the host environment. All non-native objects are host objects, such as Windows in IE and wscript instances in wscript, any class created by the user.

Related Article

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.