1. Object类自身用处不大, 不过在了解其他类之前, 还是应该了解它。JavaScript中的所有对象都由这个类继承而来, Object类中的所有属性和方法都会出现在其他对象中, 所以理解了Object类, 就可以更好地理解其他对象。
2. Object类的constructor属性
2.1. constructor属性是对创建对象的函数的引用。
2.2. Object类的constructor属性指向Function()这个构造函数。也就是说Object类是使用函数创建的。
2.3. 实例
document.write(Object.constructor + '<hr />'); // 指向Function的构造器
3. Object类的prototype属性
3.1. prototype属性对该对象的对象原型的引用。
3.2. Object原型上的7个方法:
4. Object类原型上的constructor属性
4.1. Object.prototype.constructor指向对象自身的构造器函数。
5. Object类原型上的isPrototypeOf(object)方法
5.1. isPrototypeOf()方法允许你检查一个对象是否存在于另一个对象的原型链上。表示调用对象是否在另一个对象的原型链上。
5.2. Object.prototype对象存在于obj对象的原型链上(意思就是obj继承自Object):
var obj = new Object();
document.write(Object.prototype.isPrototypeOf(obj)); // 返回true
6. Object类原型上的hasOwnProperty(property)方法
6.1. 判断对象是否有某个特定的非继承属性(不在原型链上的属性), 必须用字符串指定该属性, (例如: o.hasOwnProperty("name"))。
7. Object类原型上的propertyIsEnumerable(property)方法
7.1. propertyIsEnumerable()方法返回一个布尔值, 表示指定的属性是否可枚举。判断给定的属性是否可以用 for...in 语句进行枚举。
8. Object类原型上的toString()方法
8.1. toString()方法返回一个表示该对象的字符串。对于Object对象, toString()方法返回的是[object Object]字符串。每个对象都有toString()方法, 如果该对象没有覆盖Object对象的toString()方法, 那么返回的是[object type]字符串。
9. Object类原型上的toLocalString()方法
9.1. toLocaleString()方法返回一个该对象的字符串表示。此方法被用于派生对象为了特定语言环境的目的而重载使用。
9.2. Object的toLocaleString方法返回调用toString()的结果。
9.3. 下面几个类重写了Object的toLocaleString()方法:
- Number: Number.prototype.toLocaleString()
- Array: Array.prototype.toLocaleString()
- Date: Date.prototype.toLocaleString()
- ......
10. Object类原型上的valueOf()方法
10.1. 返回最适合该对象的原始值。
10.2. 不同类型对象的valueOf()方法的返回值:
11. 例子
11.1. 代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>Object对象</title>
</head>
<body>
<script type="text/javascript">
var easyObj = {};
var obj = new Object();
document.write('Object类的constructor属性指向Function这个构造函数: ' + Object.constructor + '<br />');
document.write('Object.constructor === Function: ' + (Object.constructor === Function) + '<hr />');
document.write('Object类的prototype属性: ' + Object.prototype + '<hr />');
document.write('Object原型上的constructor属性指向Object这个构造函数: ' + Object.prototype.constructor + '<br />');
document.write('Object.prototype.constructor === Object: ' + (Object.prototype.constructor === Object) + '<hr />');
document.write('new Object()创建的对象继承自Object: ' + Object.prototype.isPrototypeOf(obj) + '<br />');
document.write('String类继承自Object: ' + Object.prototype.isPrototypeOf(String) + '<br />');
document.write('Boolean类继承自Object: ' + Object.prototype.isPrototypeOf(Boolean) + '<br />');
document.write('Number类继承自Object: ' + Object.prototype.isPrototypeOf(Number) + '<br />');
document.write('Array类继承自Object: ' + Object.prototype.isPrototypeOf(Array) + '<br />');
document.write('Object类继承自Object: ' + Object.prototype.isPrototypeOf(Object) + '<br />');
document.write('Function类继承自Object: ' + Object.prototype.isPrototypeOf(Function) + '<hr />');
easyObj.id = 1001;
document.write('easyObj对象是否有不在原型链上的id属性: ' + easyObj.hasOwnProperty('id') + '<br />');
document.write('Object类是否有不在原型链上的constructor属性: ' + Object.hasOwnProperty('constructor') + '<br />');
document.write('Object类是否有不在原型链上的prototype属性: ' + Object.hasOwnProperty('prototype') + '<hr />');
document.write('easyObj对象是否有可枚举的id属性: ' + easyObj.propertyIsEnumerable('id') + '<br />');
document.write('easyObj对象是否有可枚举的__proto__属性: ' + easyObj.propertyIsEnumerable('__proto__') + '<hr />');
var str = new String('Java编程思想');
var boo = new Boolean(true);
var num = new Number(10000);
var arr = new Array('Java编程语言', 'Java核心技术', 99999999);
var date = new Date();
var fun = new Function("a", "b", "return a * b");
document.write(easyObj.toString() + '<br />');
document.write(str.toString() + '<br />'); // 字符串对象自己的toString方法
document.write(boo.toString() + '<br />'); // 布尔对象自己的toString方法
document.write(num.toString() + '<br />'); // 数字对象自己的toString方法
document.write(arr.toString() + '<br />'); // 数组对象自己的toString方法
document.write(date.toString() + '<br />'); // 日期对象自己的toString方法
document.write(fun.toString() + '<hr />'); // 函数对象自己的toString方法
document.write(easyObj.toLocaleString() + '<br />');
document.write(str.toLocaleString() + '<br />');
document.write(boo.toLocaleString() + '<br />');
document.write(num.toLocaleString() + '<br />'); // 数字对象自己的toLocaleString方法
document.write(arr.toLocaleString() + '<br />'); // 数组对象自己的toLocaleString方法
document.write(date.toLocaleString() + '<br />'); // 日期对象自己的toLocaleString方法
document.write(fun.toLocaleString() + '<hr />');
document.write(easyObj.valueOf() + ', ' + typeof easyObj + ', ' + typeof easyObj.valueOf() + '<br />');
document.write(str.valueOf() + ', ' + typeof str + ', ' + typeof str.valueOf() + '<br />'); // 字符串对象自己的valueOf方法
document.write(boo.valueOf() + ', ' + typeof boo + ', ' + typeof boo.valueOf() + '<br />'); // 布尔对象自己的valueOf方法
document.write(num.valueOf() + ', ' + typeof num + ', ' + typeof num.valueOf() + '<br />'); // 数字对象自己的valueOf方法
document.write(arr.valueOf() + ', ' + typeof arr + ', ' + typeof arr.valueOf() + '<br />');
document.write(date.valueOf() + ', ' + typeof date + ', ' + typeof date.valueOf() + '<br />'); // 日期对象自己的valueOf方法
document.write(fun.valueOf() + ', ' + typeof fun + ', ' + typeof fun.valueOf() + '<br /><br />');
</script>
</body>
</html>
11.2. 效果图