Js attribute Detection

Source: Internet
Author: User

Js attribute Detection

var obj = {
'X': 'xxx', 'y': 2}; var yz = obj & obj. k & obj. k. z; // in this way, no error is reported. If no definition is defined, the undefinedconsole is returned. log (yz); // undefinedyz = obj & obj. x; console. log (yz); // xxx

 

 

HasOwnProperty () does not include the attributes of the prototype chain. in case of for loop enumeration, it depends on whether PropertyIsEnumerable () can be enumerated (), getOwnPropertyDescriptor () is a good method.

 

 

Var obj = {'X': 'xxx', 'y': 2}; console. log ('x' in obj); // trueconsole. log ('tostring' in obj); // trueconsole. log (obj. hasOwnProperty ('x'); // trueconsole. log (obj. hasOwnProperty ('tostring'); // falseconsole. log (obj. propertyIsEnumerable ('x'); // trueconsole. log (obj. propertyIsEnumerable ('tostring'); // falseObject. defineProperty (obj, 'z', {enumerable: false, value: 'zzz'}); for (var item in obj) {console. log ('item --- '+ item); // x y (because other properties on the prototype chain cannot be enumerated, only x y is output)} console. log ('Z' in obj );
Console. log (Object. getOwnPropertyDescriptor (obj, 'x'); // you can check the labels in this descriptor.

 


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.