JavaScript内建对象-object对象

var arr=['a','b','c'];
console.log(Object.keys(arr));//0 1 2

var obj={
    0:'d';
    1:'e';
    2:'f';
};
console.log(Object.keys(obj));//0 1 2

var obj1=Object.create({},{
    getFoo:{
        value:function(){
            return this.foo;
        }
    }
});
obj1.foo=123;
console.log(Object.keys(obj1));//foo
//如果想获取一个对象的所有属性,包括不可枚举的,Object.getOwnPropertyNames()
console.log(Object.getOwnPropertyNames(arr));//0 1 2 length
console.log(Object.getOwnPropertyNames(obj));//0 1 2
console.log(Object.getOwnPropertyNames(obj1));//getfoo foo
var obj={get foo(){return 123;}};
console.log(Object.getOwnPropertyDescriptor(obj,'foo'));//查看属性
obj={
    name:'king';
    age:12

};
console.log(Object.getOwnPropertyDesctiptor(obj,'name'));
//只能得到自身属性描述,不能从原型上查找属性

所有对象都继承了Object.prototype的方法和属性,尽管他们可能被覆盖。

//constructor
var obj3=new Object;
console.log(obj3.constructor==Object);
var arr1=new Array;
console.log(arr1.constructor==Array);
var n=new Number(324);
console.log(n.constructor==Number);
//true

function Test(){}
var f=new Test();
console.log(f.constructor);//function Test()
console.log(f.toString());//object object
Test.prototype.toString=function(){
    return '自定义的tostring方法';
}
console.log(f.toString());//自定义的tostring方法
var toString=Object.prototype.toString;
console/log(toString.call(new Date));//object date

var obj={x:1};
console.log(obj.valueOf());//object {x:1}

 

你可能感兴趣的:(JavaScript)