Usually in the process of writing code, you will often encounter the object of the data processing. In the data processing of the object, the most frequent operations are "reference", "Value modification", "Get Keyword (attribute)". Usually the most annoying is also "get the key words", often forget how to get, here do a bit of finishing.
Now that you want to get the keyword, you have to have an object first. There are a lot of ways to create objects, and there are three ways I'm accustomed to them:
1, through the original constructor new object (), create an object, and then assign a value;
var New = "Shangguan"; testobj.age=function () { return this. Name;
2, directly new objects, not through the constructor (and directly new speed than the constructor faster!) )
var testobj== "Shangguan"; testobj.age=function () { return This . Name;};
3. Overload the constructor, which allows the constructor to be constructed on a predetermined property when constructing the object.
// to create a method for constructing an object function NEWOBJ (name, age) { this. Name = name; this. age= age ; This function () { returnthis. Name; }} // Create an Object var New newObj ("Shangguan", 25);
1. Object built-in property method:Object.keys (); The method returns an array that includes the enumerable properties within the object and the method name . The order in which the property names are arranged in the array for...in
is consistent with the order that is returned when the object is traversed.
Gets the defined (enumerable) properties and methods on the object by calling the Object.keys () method
var keys=// output arr ["Name", "Age", "action"]
Note: in ES5, if the parameter of this method is not an object (but a primitive value), then it throws TypeError. In ES2015, the parameters of the non-object are cast to an object.
Object.keys ("Testobj"); // TypeError: "Testobj" is not a object (ES5 code) Object.keys ("Testobj"); //["Name", "Age", "action"] (ES2015 code)
2,//object.getownpropertynames () returns all properties of the array (enumerable or non-enumerable) directly to the given object.
All properties of the returned array (enumerable or non-enumerable) are directly found for the given object.
Console.log ("attr", Object.getownpropertynames (Mytester)); Output attr ["Name", "attr", "Sayhi"]
Add a property to the Object prototype
Object.prototype.newShine = "It ' s Me";
Returns an enumerable property that has been found in the prototype chain of the object
for (var i in Mytester) {
Console.log (i);
}
Output Name,attr,sayhi,newshine
Returns an enumerable property that is directly defined on the object
for (var i in Mytester) {
if (Mytester.hasownproperty (i)) {
Console.log (i);
}
}
Output Name,attr,sayhi
Object.keys (), Object.getownpropertynames (), for...in ... Contrast
Non-enumerable object properties
var nonenum = Object.create ({}, {
Getfoo: {
Value:function () {
return this.foo;
},
Enumerable:false
}
});
Nonenum.foo = 1;
NONENUM.ASJ = 2;
Gets an enumerable or non-enumerable property of an object
Console.log (Object.getownpropertynames (nonenum). sort ()); Output ["ASJ", "foo", "Getfoo"]
Gets an enumerable property of an object
Console.log (Object.keys (nonenum). sort ()); Output ["ASJ", "foo"]
Returns an enumerable property that is directly defined on the object
for (var i in Nonenum) {
if (Nonenum.hasownproperty (i)) {
Console.log (i); Output Foo ASJ
}
}
Get the JavaScript object property name and method name, respectively
To create a method for constructing an object
function MYOBJ (name, attr) {
THIS.name = name;
This.attr = attr;
This.sayhi = function () {
return ' Hi everyone!!! ';
}
}
Create an Object
var mytester = new MYOBJ ("Shinejaie", 1)
Get Object methods
for (var i in Mytester) {
if (Mytester.hasownproperty (i) && typeof mytester[i] = = "function") {
Console.log ("Object method:", I, "=", Mytester[i])
}
}
Output Object method: Sayhi = () {return ' Hi everyone!!! ';}
Get Object Properties
for (var i in Mytester) {
if (Mytester.hasownproperty (i) && typeof mytester[i]! = "function") {
Console.log ("Object properties:", I);
}
}
Output Object Properties: Name Object properties: attr
JS getting the Object "properties and Methods" method