该属性指向用来构造该函数对象的构造器,在这里为Object()
Object.prototype.constructor === Object; // true
var o = new Object();
o.constructor === Object; // true
Object.prototype.toString(radix)
该方法返回的是一个用于描述目标对象的字符串。特别地,当目标是一个 Number 对象时,可以传递一个用于进制数的参数radix
,该参数radix
,该参数的默认值为 10。
var o = {prop:1};
o.toString(); // ‘[object Object]’
var n = new Number(255);
n.toString(); // ‘255’
n.toString(16); // ‘ff’
Object.prototype.toLocaleString()
该方法的作用与toString()
基本相同,只不过它做一些本地化处理。该方法会根据当前对象的不同而被重写,例如Date()
,Number()
,Array()
,它们的值都会以本地化的形式输出。当然,对于包括Object()
在内的其他大多数对象来说,该方法与toString()
是基本相同的。在浏览器环境下,可以通过BOM
对象Navigator
的language
属性(在IE
中则是userLanguage
)来了解当前所使用的语言:
navigator.language; //‘en-US’
Object.prototype.valueOf()
该方法返回的是用基本类型所表示的this
值,如果它可以用基本类型表示的话。如果Number
对象返回的是它的基本数值,而Date
对象返回的是一个时间戳(timestamp
)。如果无法用基本数据类型表示,该方法会返回this
本身。
// Object
var o = {};
typeof o.valueOf(); // ‘object’
o.valueOf() === o; // true
// Number
var n = new Number(101);
typeof n; // ‘object’
typeof n.vauleOf; // ‘function’
typeof n.valueOf(); // ‘number’
n.valueOf() === n; // false
// Date
var d = new Date();
typeof d.valueOf(); // ‘number’
d.valueOf(); // 1503146772355
Object.prototype.hasOwnProperty(prop)
该方法仅在目标属性为对象自身属性时返回true
,而当该属性是从原型链中继承而来或根本不存在时,返回false
。
var o = {prop:1};
o.hasOwnProperty(‘prop’); // true
o.hasOwnProperty(‘toString’); // false
o.hasOwnProperty(‘formString’); // false
Object.prototype.isPrototypeOf(obj)
如果目标对象是当前对象的原型,该方法就会返回true
,而且,当前对象所在原型上的所有对象都能通过该测试,并不局限与它的直系关系。
var s = new String(‘’);
Object.prototype.isPrototypeOf(s); // true
String.prototype.isPrototypeOf(s); // true
Array.prototype.isPrototypeOf(s); // false
Object.prototype.propertyIsEnumerable(prop)
如果目标属性能在for in
循环中被显示出来,该方法就返回true
var a = [1,2,3];
a.propertyIsEnumerable(‘length’); // false
a.propertyIsEnumerable(0); // true
三、在ES5
中附加的Object
属性
在ES3
中,除了一些内置属性(如:Math.PI
),对象的所有的属性在任何时候都可以被修改、插入、删除。在ES5