Find out if object obj is not on the prototype chain (note that there is also a space behind the colon of the test example)
1, returns the array, the format is Key:value
2, the result array does not require the order
Enter an example:
var C = function () {This.foo = ' bar '; this.baz = ' Bim ';}; C.prototype.bop = ' Bip '; Iterate (new C ());
Output Example:
["Foo:bar", "Baz:bim"]
Method One: for...in + hasownproperty can use for-in to traverse the properties in the object, hasOwnProperty method can return a Boolean value,
Indicates whether an object has a property of the specified name。
This method cannot check whether the object's prototype chain has this property, which must be a property of the object itself.
function iterate (obj) {
var arr = [];
Use For-in to traverse object Properties
for (var key in obj) {
//To determine whether the key is the property of the object itself
if (Obj.hasownproperty (key)) {
// Store attributes and values in an array by format
Arr.push (key+ ": +obj[key]);
}
return arr;
}
Method Two:
function iterate (obj) {return
object.getownpropertynames (obj). Map (function (key) {return
key+ ":" +obj[key ];
});
}