(Translation) ECMAScript 5 Objects and Properties (1)

Source: Internet
Author: User
Document directory
  • Objects
  • Original post: http://ejohn.org/blog/ecmascript-5-objects-and-properties/

 @ By: Aaron

Some new APIs are included in the specifications, but more interesting functions play a role in the object/attribute code. How can we use them to function your objects, use private "getters" and "settses" to prevent enumeration, operations, or deletion, or even adding new properties. in short, you will be able to copy and extend existing JavaScript-Based APIS (such as DOM) to use only JavaScript itself.

Perhaps most importantly, though: these features will arrive in all major browsers. All major browser vendors engage in specification and agree to implement in their respective engines. But the exact schedule is unclear, but it will be earlier, not later.

ES5 does not seem to have a complete implementation yet, but there are some works. You can read the ECMAScript 5 specification during this period.

Objects

A new feature of ECMAScript 5 is that the extended object can be switched. Disabling scalability prevents new attributes from being added to an object

Object.preventExtensions( obj )
Object.isExtensible( obj )

PreventExtensions locks an object to prevent conflicts with future attributes.

IsExtensible is a method to determine the scalability of the current object.

Example usage:

var obj = {}; obj.name = "John";print( obj.name );// John print( Object.isExtensible( obj ) );// true Object.preventExtensions( obj ); obj.url = "http://ejohn.org/"; // Exception in strict mode print( Object.isExtensible( obj ) );// false

Attributes and Descriptors:

Attributes have been thoroughly overturned. It is no longer a simple value-associated object-you have full control over how it works. Using this capability, though, increases complexity.

Object Attributes are divided into two parts.

There are two possibilities for an actual "meat" attribute: one value (we know that ECMAScript 3 is a "data" Attribute-this is a traditional value) or a Getter and Setter (we know from some modern browsers that a "access" attribute is like Gecko and WebKit ).

  • Value contains the Value of this attribute.

 

  • When the Get function is called, the value of this attribute is accessed.
  • Set calls the function. The value of this attribute changes.

In addition, attributes can be...

  • Writable. If it is false, the value of this attribute cannot be changed.
  • Retriable. If it is flase, any attempt to delete this attribute or change its properties (Writable, retriable, or Enumerable) will fail.
  • Enumerable. If it is true, when for (var prop in obj) {} (with other similar functions) is used, this attribute can be traversed.

These different attributes completely make up for the attribute descriptor. For example, a simple descriptor may be similar to the following:

{  value: "test",  writable: true,  enumerable: true,  configurable: true}

The three attributes (writable, enumerable, and retriable) are optional, and all attributes are true by default.

You can use newThe Object. getOwnPropertyDescriptor method obtains its information.

Object.getOwnPropertyDescriptor( obj, prop )

This method allows you to access the attributes of the descriptor. This method is the only way to obtain this information (otherwise, it is not provided to the user-these attributes that do not have visible attributes, they are stored in the internal ECMAScript engine ).

Example:

var obj = { foo: "test" }; print(JSON.stringify(  Object.getOwnPropertyDescriptor( obj, "foo" )));// {"value": "test", "writable": true,//  "enumerable": true, "configurable": true}

Object.defineProperty( obj, prop, desc )

This method allows you to define a new attribute on an object (or change the existing attribute). This method accepts an attribute descriptor to initialize (update) the attribute.

Example:

var obj = {}; Object.defineProperty( obj, "value", {  value: true,  writable: false,  enumerable: true,  configurable: true}); (function(){  var name = "John";   Object.defineProperty( obj, "name", {    get: function(){ return name; },    set: function(value){ name = value; }  });})(); print( obj.value )// true print( obj.name );// John obj.name = "Ted";print( obj.name );// Ted for ( var prop in obj ) {  print( prop );}// value// name obj.value = false; // Exception if in strict mode Object.defineProperty( obj, "value", {  writable: true,  configurable: false}); obj.value = false;print( obj.value );// false delete obj.value; // Exception

Object. defineProperty is a core method in the new ECMAScript version. Almost all other major functions depend on this method.

Object.defineProperties( obj, props )

 

It means defining some attributes at the same time (instead of a separate attribute)

Object.defineProperties = function( obj, props ) {  for ( var prop in props ) {    Object.defineProperty( obj, prop, props[prop] );  }};
var obj = {}; Object.defineProperties(obj, {  "value": {    value: true,    writable: false  },  "name": {    value: "John",    writable: false  }});

In the new function of ECMAScript5, attribute Descriptors (and related methods) may be the most important. it enables developers to have fine-grained control over their objects, prevent unwanted repairs and supplements, and maintain a unified network-compatible API.

 

 

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.