object.keys
object.keys
object.keys : 回傳物件所有属性與方法
var obj = {'a':'123','b':'345'};
console.log(Object.keys(obj)); //['a','b']
var obj1 = { 100: "a", 2: "b", 7: "c"};
console.log(Object.keys(obj1)); // console: ["2", "7", "100"]
var obj2 = Object.create({}, { getFoo : { value : function () { return this.foo } } });
obj2.foo = 1;
console.log(Object.keys(obj2)); // console: ["foo"]
2.object.hasOwnProperty('Property') : 回傳true/false
var myObject = {
name: '@tips_js'
};
myObject.hasOwnProperty('name'); // true
'name' in myObject; // true
myObject.hasOwnProperty('valueOf'); // false, valueOf 繼承自原型鏈結
'valueOf' in myObject; // true
//在 in 運算符沒辦法分辨之間的屬性是建立在物件或是繼承自原型鏈結的。
3.get value Property
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
};
//1.objectName.propertyName
person.lastName; //Doe
//2.objectName["propertyName"]
person["lastName"]; //Doe