JS Object-Property traversal

Source: Internet
Author: User
Tags hasownproperty

In order to strengthen the cognition of JS object, and brush a question.

Directly on the topic:

Find out which object obj is not on the prototype chain (note that there is also a space after the colon of the test example)
1, return array, formatted as Key:value
2. The result array does not require order

Example:

// input var function () {this. Baz = ' bim' = ' Bip '; iterate (new  C ()); // Output ["Foo:bar", "Baz:bim"]

function body
function iterate (obj) {
Content
}

The problem-solving idea, iterate the function of the received obj, first printed out.

{foo: "Bar", Baz: "Bim"}

The output array is simple.

For in loop, modify the Iterate function

function Iterate (obj) {  for (var in obj) {       // is used in brackets here because, key is a  quoted character       return key + ":" + Obj[key]              }  }// return Foo:barbaz:bimbop:bip

How many more, but when I print obj, there are only two of them.

Look under for in, just iterate through an enumerable property of an object .

What is an enumerable property ?

An enumerable property is a property whose internal enumerable flag is set to true, which defaults to true for properties initialized with a direct assignment and property. An enumerable property can be traversed through a for...in loop (unless the property name is a Symbol). Ownership of a property is determined by determining whether the property is directly part of an object, rather than inheriting it through the prototype chain. All properties of an object can be obtained at once. There are built-in methods for judging, iterating/enumerating, and getting one or a set of properties for an object, which are enumerated in the following table. For some categories that are not available, the following example code demonstrates how to get a method.

RELATED Links: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Enumerability_and_ownership_of_properties

Personal understanding: The For In loop is an enumerable property, which is actually the value of the object initialization, and later passed directly to the value Xx.proprototype.one = ' 11 ' This way, if you want to access

Console.log (obj.__proto__) // return  "Bip", constructor:?}

Judging non-enumerable properties

In: Returns a Boolean value that returns True

if inch Bop) {  //push into array   }

propertyIsEnumerable (): Returns a Boolean value that returns True

if (!obj[key].propertyisenumerable (' Bip ')) {  // next execution   }

hasOwnProperty()Method returns a Boolean value indicating whether the property in the object's own property has the specified attribute

if (!key.hasownproperty (' Bip ')) {  // continue execution   }

Object.getOwnPropertyNames()method returns an array of property names of all of the properties of the specified object, including properties that do not include the symbol value as the name of the non-enumerable property.

return Object.getownpropertynames (obj). Map (function  (key) {   //    return key + ":" + Obj[key]})

Object.getOwnPropertyNames()Returns an array that is a obj string of enumerated or non-enumerable property names owned by the element itself. The order of the enumerated attributes in the array is consistent with iterating through for...in Object.keys the object properties (or). The order of non-enumerable properties in the array is undefined.

return Object.getownpropertynames (obj). Map (function  (key) {   //    return key + ":" + Obj[key]})

These are the official recommended methods, which can be seen in the links above.

Can implement the above topic, there may be some compatibility issues, resulting in a commit error.

This is used hasOwnProperty and Getownpropertynames can be submitted, do not know what the test is exactly what the content, Object.keys (obj), with him to loop push in, because the returned array is three, and the value is 4, The fourth one is added to the back, so this way is also possible, here is recommended a better method.

Getownpropertynames

return    Object.getownpropertynames (obj). Map (functionreturn key + ': ' + Obj[key]; });

A small topic, the basic knowledge of the object is a test.

JS Object-Property traversal

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.