Object. defineproperty interface browser implementation bug. and doubts

Source: Internet
Author: User


Let's take a look at the description on msdn.

Adds a property to an object, or modifies attributes of an existing property.

 
Object. defineproperty (object, propertyname, descriptor)
 

Arguments

Object

Required. The object on which to add or modify the property. This can be a native JavaScript Object or a DOM object.

 (Can be is not equal to must be. What exactly does IE8 do? In addition, Safari does not allow DOM objects .)

Propertyname

Required. A string that contains the property name.

Descriptor

Required. A JavaScript object that is a descriptor that describes the property. The definition can be for a data property or an accessor property.

Return valuethe object that was passed to the function.


Remarks

You can use

Object. defineproperty function to do the following:

    • Add a new property to an object. This occurs when the object does not have the specified property name.

    • Modify attributes of an existing property. This occurs when the object already has the specified property name. (Refer to Note 1 below)

The property definition is provided in a descriptor object, which describes the attributes of a Data property or an accessor property. The descriptor object is a parameter of theobject. defineproperty function.


Descriptor parameters: (see section ecma262 edition5 8.12.9)


Data descriptor attribute

Description

Default if not specified when you add a property

Value

The current value of the property.

Undefined

Writable

True or false. If writable is set to true, the property value can be changed.

False

Enumerable

True or false. If enumerable is set to true, the property can be enumerated by a... In statement.

False

Retriable

True or false. If retriable is set to true, property attributes can be changed, and the property can be deleted.

False



BUG:

1. IE8. This interface cannot be used for common new objects.
VaR o = {};
Object. defineproperty (O, 'abc', {value: 123 }); //Throw an exception, not because o is a common JS object.
O. ABC = 300
Alert (O. ABC );
If O = window or some DOM elements and other host objects, you can use this interface. but not all the host objects can. another important issue is. for the attributes parameter by default, IE8 does not implement the default read-only of the ABC attribute of O. even if you explicitly add writable: false. this is also true. I don't know what else this interface is implemented by IE8 as a bird. 2. IE8 retriable: false


VaR o = window;
Object. defineproperty (O, 'abc', {value: 123, retriable: false });
Delete o. // IE8 throws an exception.
Alert (O. ABC );


That is to say, resumable has no meaning at all. The phenomenon is like an inherent bug. ReferenceCode:
Window. O = 123; Delete window. O; //IE8Throw an exception. Likewise, the object does not support this operation.


3. OBJ simulates const for the window Host object.
Object. defineproperty (window, 'abc', {value: 123, writable: false });
Abc= 200;
Window. abc= 300;
Alert ([ABC, window. ABC]) // IE8, safari5 300,300 | chrome, ie9 123,123
This involves a very depressing issue. IE8 sucks, I can understand, but what is the excitement of safari? Fortunately, if I want to simulate const, safari4 has long supported it. So we can assume that ie9 + can use this to simulate Const. Of course, Safari does not have this problem if the window is replaced with a common object, so this should be a bug that safari implements this interface for the host object. Note 1: I am still confused about this issue. At least from the definition of ecma62 edition5, I feel that all browsers do not comply with the standards. Or I have incorrect understanding of the standards. Reference code:
VaR o = {ABC: 123 };

Object. defineproperty (O, 'abc', {value: 200,WRitable: false}); // writable instead of writable. If it is written in uppercase, it is considered as the default feature. Then isdatadescriptor will be false.
O. ABC = 300;
Alert (O. ABC)// All browsers supporting defineproperty print 300. That is, writable: false, which is invalid.


For the attribute set by the [[put] internal method of object O (O. ABC = 123 or JSON In the example). This attribute is a data property. (unlike accessor property and setter getter property .) its feature set (writable, retriable, enumerable) will be set to true. the descriptor object {value: 200, writable: false} has the value feature. the result is the internal operation of isdatadescriptor for both of them. The result is true for both of them. the final step is 10. B (Else, the [[writable able] field of current is true, so any change is acceptable. Refer to Chapter 8.12.9). That is, new features should be applied. Then I am confused by the results of the test code. Observe the steps of defineownproperty. it is found that only in Step 9. b. i. converts the data property ABC to an accessor property. feature operations are ignored. however, Step 9 is required. at least one Descriptor and {value: 200, writable: false} in the ABC attribute returns false when performing the isdatadescriptor operation. however, this is obviously not possible in the test code. so I am very confused. I hope that my understanding of standards is incorrect. not all browser implementations are problematic. I hope I can find the answer to this question earlier. Finally, I have an answer to this question. hax points out my tragedy. the feature name of my descriptor, whose first letter is capitalized. therefore, it is ignored by default. so it actually enters 9. b. I have. in the past, all feature names were written in uppercase. The reason why it seems to take effect is that the default value is false. Tragic Life... leave this post to commemorate my tragedy.

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.