JavaScript advanced programming (version 3rd) Study Notes 10 access JavaScript objects

Source: Internet
Author: User

1. Recognition of Objects

(1) object attributes and features

What is Property and Attribute? What is the difference? I don't want to distinguish it from semantics. For this series of articles, attributes constitute a part of the object and also include object methods in a broad sense, the feature refers to the characteristics of the described subject. In other words, the attribute is the specific existence that we can access through encoding, and the feature is mainly for the convenience of understanding the abstract existence of concepts, of course, features can also be externalized through corresponding attributes. This section describes the characteristics of object attributes, mainly from the 5th edition of ECMA-262 specification, this specification uses two brackets to describe internal features that cannot be accessed directly.

A. attribute type (attribute is classified into the following classes first ):

  • Data property: directly access the property of the property value
  • Accessors: Use the getter/setter method to access attributes of the property value.
  • Internal Attributes: attributes that cannot be directly accessed by code exist only for the purpose of Specification Description. Two brackets are also used to describe the attributes in the specification.

B. Internal properties of Objects

Internal Attributes cannot be directly accessed through code. They are mainly used to describe the specifications and are also for reference by ECMAScript implementers. for developers, understanding these attributes makes it easy to understand some internal mechanisms. For example, when assigning a value to an attribute, the [[Put] internal method is called in the implementation. When reading an attribute value, the [[Get] method is called.

Public Internal Attributes of all objects Exclusive Internal Attributes of individual objects
Name Specifications Name Specifications Object
[[Prototype] Object/Null [[PrimitiveValue] Primitive Boolean | Date | Number | String
[[Class] String [[Construct] SpecOp (a List of any) → Object New
[[Extensible] Boolean [[Call] SpecOp (any, a List of any) → any | Reference Call
[[Get] SpecOp (propName) → any [[HasInstance] SpecOp (any) → Boolean Function
[[GetOwnProperty] SpecOp (propName) → Undefined | Property Descriptor [[Scope] Lexical Environment Function
[[GetProperty] SpecOp (propName) → Undefined | Property Descriptor [[FormalParameters] List of Strings Function
[[Put] SpecOp (propName, any, Boolean) [Copy codeThe Code is as follows:] ECMAScript code Function
[[CanPut] SpecOp (propName) → Boolean [[TargetFunction] Object Function. prototype. bind
[[HasProperty] SpecOp (propName) → Boolean [[BoundThis] Any Function. prototype. bind
[[Delete] SpecOp (propName, Boolean) → Boolean [[BoundArguments] List of any Function. prototype. bind
[[DefaultValue] SpecOp (Hint) → primitive [[Match] SpecOp (String, index) → MatchResult RegExp
[[DefineOwnProperty] SpecOp (propName, PropDesc, Boolean) → Boolean [[ParameterMap] Object

Note:

  • Every Object has a Prototype Object [[Prototype]. Generally, we cannot directly access this internal attribute in the code, but we can use the Object. getPrototypeOf (object) to obtain the prototype object (in Firefox, you can directly access it through _ proto ).
  • In Object. prototype. in the toString method, the built-in object will return the value "[object Class]" containing [[class] According to the specification. the [[Class] value of the built-in Object is the corresponding name (for example, the [[Class] value of the Array Object is 'array'). Therefore, you can use the Object. prototype. toString. call (value) = '[object Array]' to determine whether the value is an Array.
  • When assigning a value to an attribute, the backend calls [[Put] to implement it. When obtaining an attribute value, the backend calls [Get] to obtain it.
  • When a function is called using the new operator, [[Construct] is called in the background. When the call method is used to Call the function, [[call] is called in the background.
  • [[HasInstance] The method returns whether a given parameter is created by calling a function, which is similar to the isPrototypeOf (obj) method of the Object.
  • When a function is executed, a [[Scope] object is created. [[Scope] is the activity object we mentioned earlier, that is to say, this, arguments, form parameters, variables and functions defined in the function are all attributes of the [Scope] object.

C. Attribute features (used to describe the attributes)

Internal features Configure attributes Attribute type Data Type Default Value Description Remarks
[[Retriable] Retriable

Data attributes

Accessors

Boolean

True

Can I delete a property through delete to redefine the property?

Can attributes be modified?

Can I change the attribute to the accessors?

Once the attribute is defined as not configurable, it cannot be changed to configurable.

If this parameter is set to false, the attribute cannot be deleted or modified, but the attribute value can be modified.

Operations are ignored in non-strict mode, and exceptions are thrown in strict mode.

[[Enumerable] Enumerable

Data attributes

Accessors

Boolean True Can I return attributes through a for-in loop? If it is true, it can be enumerated by for-in. Otherwise, it cannot be enumerated by for-in.
[[Writable] Writable Data attributes Boolean True Can I modify the attribute value? When the value is false, the attribute value cannot be modified. operations are ignored in non-strict mode, and an exception is thrown in strict mode.
[[Value] Value Data attributes Any Type Undefined Attribute Value
[[Get] Get Accessors Undefined/Function Undefined Function called when reading an attribute When it is a function, no parameter is required to call this function, and the return value is returned as the property value.
[[Set] Set Accessors Undefined/Function Undefined Function called when writing an attribute When it is a function, it calls this function as a parameter and assigns it to the attribute.

Note:

  • Configuration Attribute refers to the configuration item name used to define relevant features when using the attribute definition method described below.
  • [[Get] and [[Set] may not be available for accessors. attributes without [Get] cannot be read (undefined is returned, an exception is thrown in strict mode. If the attribute does not have [Set], it cannot be written (it is ignored, and an exception is thrown in strict mode ).
  • Note that the characteristics of object Internal Attributes and object attributes are differentiated.

D. Attribute definition method (used to define attributes)

The most common way to define attributes is to add attributes directly on the object, such as obj. name = 'linjisong'. In this case, all internal properties of the defined attribute are default. What should I do if I want to define an attribute whose value cannot be modified? ES provides several methods for implementing similar functions.

Method Name Function Description Parameters and return values Description Call example
DefineProperty () Define an attribute

(1) target object

(2) attribute name

(3) attribute descriptor object

When using the attribute definition method
[[Enumerable]
[[Retriable]
[[Writable]
The default value is false.

// Create an object that contains a default job attribute (the job attribute can be modified, deleted, and enumerated in for-in)
Var person = {job: 'it '};
// Add a name attribute that cannot be modified or deleted
Object. defineProperty (person, 'name ',{
Value: 'linjisong', // The Configuration Attribute here is consistent with the configuration attribute in the above feature list
Enumerable: true
});
// Define multiple attributes (data attributes year and accessors attributes age)
Object. defineProperties (person ,{
Year :{
Value: 2012,
Retriable: true,
Writable: true
},
Age :{
Get: function (){
Return this. year-1983;
}
}
});

Var job = Object. getOwnPropertyDescriptor (person, 'job ');
Console.info (job. retriable); // true. The default value is true when attributes are directly added.

Var name = Object. getOwnPropertyDescriptor (person, 'name ');
Console.info (name. retriable); // false. The default value is false when attributes are added using the attribute definition method.
Console.info (person. name); // linjisong
Person. name = 'external'; // The value is not changed because it cannot be modified. An exception is thrown in strict mode.
Console.info (person. name); // linjisong

Person. year = 2015;
Console.info (person. year); // 2015
Console.info (person. age); // 32. The age attribute is modified when year is modified.

DefineProperties () Define a set of attributes

(1) target object

(2) An object composed of multiple attribute Descriptors

GetOwnPropertyDescriptor () Get attributes

(1) target object

(2) attribute name

(3) returns an object that includes the property features.

Note: The attributes set or obtained by these methods are related to the attribute type, for example, you can only set [[Confirurable], [[Enumerable], [[Writable], and [Value] for data properties.

(2) Tamper-proofing objects

The so-called anti-tampering object is to provide a certain level of protection for the object to prevent changes to the object at this level. In ES5 norms, three protection levels are defined:

Protection Level Description Procedure Judgment Method Description
Not scalable You cannot add new attributes and methods to an object, but you can modify existing attributes and methods. PreventExtensions () IsExtensible (): false
Seal The attribute cannot be expanded, and the [[resumable] of an existing member is set to false. The attribute cannot be deleted, but the attribute value can be modified. Seal () IsSeal (): returns true if it is sealed When isSeal () is true, isExtensible () must be false.
Freeze The [[Writable] parameter is Set to false. However, if [[Set] is defined, the accessors attribute can still be written. Freeze () IsFrozen (): returns true when frozen When isFrozen () is true, isSeal () must be true, and isExtensible () must be false.

Note: once defined as a tamper-resistant object, it cannot be undone.

(3) Other methods of Objects

Name Description
Create (prototype [, descriptors]) Creates an object that has a specified prototype and can selectively contain the specified attribute.
GetOwnPropertyNames (object) Returns the property (method) name of an object.
GetPrototypeOf (object) Returned object prototype
Keys (object) Returns the name of the enumerated property (method) of the object.

Here, create (prototype [, descriptors]) is a very interesting method, which describes its behavior in the specification as follows:

[Code]
① If prototype is not Null or Object, a TypeError exception is thrown.
② Var obj = new Object ()
③ Set the internal attribute [[Prototype] of obj to prototype.
④ If descriptors exists and are not undefined, use Object. defineProperties (obj, descriptors) to add attributes.
⑤ Return obj

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.