@ Wu Qiong Adam (Sina Weibo)
In the process of learning JavaScript, it is often confused by its Object system (Object, Function, Prototype). If you do not understand these things, subsequent JavaScript learning will not be solid, so make up your mind to understand it. Let's get started!
During this learning process, The main bibliography is JavaScript: The Good Parts, JavaScript: The Definitive Guide, and Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications, and libraries; the main analysis tools are IE and IE Developer Tools.
1. Object
JavaScript language does not have the concept of classes. Except for basic types (Number, String, booleans, null, and undefined), everything is an object. The object definition is as follows, which is more accurate in English:
Objects are mutable keyed collections.
An object is a container of properties, where a property has a name and a value.
A property name can be any string, including the empty string.
A property value can be any JavaScript value before t for undefined.
These are not difficult to understand. The key is:
Object names des a prototype linkage feature that allows one object to inherit the properties of another.
Here, this prototype link is hidden (invisible in the Debugger). In some implementations, its attribute name is _ proto __
So far, the domain model after the JavaScript Object System is modeled using UML is as follows:
Additionally, the _ proto _ link of any Object is not empty. For an Object created from Object Literal, its _ proto _ points to Object. prototype.
With the _ proto _ link, you can exert some object-oriented power. The first one is Delegation. Delegation means that when an attribute fails to be obtained from an object, JS will automatically try to find it in the object indicated by its _ proto _ link. If it still cannot be found, follow _ proto _ to find the Object. prototype. According to the description of the preceding Delegation algorithm, you can easily understand that the Delegation process is dynamic. This time, you can find the attribute from the ancestor, which does not necessarily mean that you can find it next time.
Note that the __proto _ link is only valid for queries, but not for Update. That is to say, if you assign a value to an attribute, this property does not exist in this object, but the _ proto _ ancestor already has an attribute of the same name. However, JS will create another attribute in this object instead of modifying the attribute value of the ancestor.
From column adwu73