047_Object对象
1. Object類自身用處不大, 不過在了解其他類之前, 還是應(yīng)該了解它。JavaScript中的所有對(duì)象都由這個(gè)類繼承而來, Object類中的所有屬性和方法都會(huì)出現(xiàn)在其他對(duì)象中, 所以理解了Object類, 就可以更好地理解其他對(duì)象。
2. Object類的constructor屬性
2.1. constructor屬性是對(duì)創(chuàng)建對(duì)象的函數(shù)的引用。
2.2. Object類的constructor屬性指向Function()這個(gè)構(gòu)造函數(shù)。也就是說Object類是使用函數(shù)創(chuàng)建的。
2.3. 實(shí)例
document.write(Object.constructor + '<hr />'); // 指向Function的構(gòu)造器3. Object類的prototype屬性
3.1.?prototype屬性對(duì)該對(duì)象的對(duì)象原型的引用。
3.2.?Object原型上的7個(gè)方法:
4. Object類原型上的constructor屬性
4.1. Object.prototype.constructor指向?qū)ο笞陨淼臉?gòu)造器函數(shù)。
5. Object類原型上的isPrototypeOf(object)方法
5.1. isPrototypeOf()方法允許你檢查一個(gè)對(duì)象是否存在于另一個(gè)對(duì)象的原型鏈上。表示調(diào)用對(duì)象是否在另一個(gè)對(duì)象的原型鏈上。
5.2. Object.prototype對(duì)象存在于obj對(duì)象的原型鏈上(意思就是obj繼承自O(shè)bject):
var obj = new Object(); document.write(Object.prototype.isPrototypeOf(obj)); // 返回true6. Object類原型上的hasOwnProperty(property)方法
6.1. 判斷對(duì)象是否有某個(gè)特定的非繼承屬性(不在原型鏈上的屬性), 必須用字符串指定該屬性, (例如: o.hasOwnProperty("name"))。
7. Object類原型上的propertyIsEnumerable(property)方法
7.1. propertyIsEnumerable()方法返回一個(gè)布爾值, 表示指定的屬性是否可枚舉。判斷給定的屬性是否可以用 for...in 語句進(jìn)行枚舉。
8. Object類原型上的toString()方法
8.1. toString()方法返回一個(gè)表示該對(duì)象的字符串。對(duì)于Object對(duì)象, toString()方法返回的是[object Object]字符串。每個(gè)對(duì)象都有toString()方法, 如果該對(duì)象沒有覆蓋Object對(duì)象的toString()方法, 那么返回的是[object type]字符串。
9. Object類原型上的toLocalString()方法
9.1. toLocaleString()方法返回一個(gè)該對(duì)象的字符串表示。此方法被用于派生對(duì)象為了特定語言環(huán)境的目的而重載使用。
9.2. Object的toLocaleString方法返回調(diào)用toString()的結(jié)果。
9.3. 下面幾個(gè)類重寫了Object的toLocaleString()方法:
- Number: Number.prototype.toLocaleString()
- Array: Array.prototype.toLocaleString()
- Date: Date.prototype.toLocaleString()
- ......
10. Object類原型上的valueOf()方法
10.1. 返回最適合該對(duì)象的原始值。
10.2. 不同類型對(duì)象的valueOf()方法的返回值:
11. 例子
11.1. 代碼
<!DOCTYPE html> <html lang="zh-CN"><head><meta charset="utf-8" /><title>Object對(duì)象</title></head><body><script type="text/javascript">var easyObj = {};var obj = new Object();document.write('Object類的constructor屬性指向Function這個(gè)構(gòu)造函數(shù): ' + Object.constructor + '<br />');document.write('Object.constructor === Function: ' + (Object.constructor === Function) + '<hr />');document.write('Object類的prototype屬性: ' + Object.prototype + '<hr />');document.write('Object原型上的constructor屬性指向Object這個(gè)構(gòu)造函數(shù): ' + Object.prototype.constructor + '<br />');document.write('Object.prototype.constructor === Object: ' + (Object.prototype.constructor === Object) + '<hr />');document.write('new Object()創(chuàng)建的對(duì)象繼承自O(shè)bject: ' + Object.prototype.isPrototypeOf(obj) + '<br />');document.write('String類繼承自O(shè)bject: ' + Object.prototype.isPrototypeOf(String) + '<br />');document.write('Boolean類繼承自O(shè)bject: ' + Object.prototype.isPrototypeOf(Boolean) + '<br />');document.write('Number類繼承自O(shè)bject: ' + Object.prototype.isPrototypeOf(Number) + '<br />');document.write('Array類繼承自O(shè)bject: ' + Object.prototype.isPrototypeOf(Array) + '<br />');document.write('Object類繼承自O(shè)bject: ' + Object.prototype.isPrototypeOf(Object) + '<br />');document.write('Function類繼承自O(shè)bject: ' + Object.prototype.isPrototypeOf(Function) + '<hr />');easyObj.id = 1001;document.write('easyObj對(duì)象是否有不在原型鏈上的id屬性: ' + easyObj.hasOwnProperty('id') + '<br />');document.write('Object類是否有不在原型鏈上的constructor屬性: ' + Object.hasOwnProperty('constructor') + '<br />');document.write('Object類是否有不在原型鏈上的prototype屬性: ' + Object.hasOwnProperty('prototype') + '<hr />');document.write('easyObj對(duì)象是否有可枚舉的id屬性: ' + easyObj.propertyIsEnumerable('id') + '<br />');document.write('easyObj對(duì)象是否有可枚舉的__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核心技術(shù)', 99999999);var date = new Date();var fun = new Function("a", "b", "return a * b");document.write(easyObj.toString() + '<br />');document.write(str.toString() + '<br />'); // 字符串對(duì)象自己的toString方法document.write(boo.toString() + '<br />'); // 布爾對(duì)象自己的toString方法document.write(num.toString() + '<br />'); // 數(shù)字對(duì)象自己的toString方法document.write(arr.toString() + '<br />'); // 數(shù)組對(duì)象自己的toString方法document.write(date.toString() + '<br />'); // 日期對(duì)象自己的toString方法document.write(fun.toString() + '<hr />'); // 函數(shù)對(duì)象自己的toString方法document.write(easyObj.toLocaleString() + '<br />');document.write(str.toLocaleString() + '<br />');document.write(boo.toLocaleString() + '<br />');document.write(num.toLocaleString() + '<br />'); // 數(shù)字對(duì)象自己的toLocaleString方法document.write(arr.toLocaleString() + '<br />'); // 數(shù)組對(duì)象自己的toLocaleString方法document.write(date.toLocaleString() + '<br />'); // 日期對(duì)象自己的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 />'); // 字符串對(duì)象自己的valueOf方法document.write(boo.valueOf() + ', ' + typeof boo + ', ' + typeof boo.valueOf() + '<br />'); // 布爾對(duì)象自己的valueOf方法document.write(num.valueOf() + ', ' + typeof num + ', ' + typeof num.valueOf() + '<br />'); // 數(shù)字對(duì)象自己的valueOf方法document.write(arr.valueOf() + ', ' + typeof arr + ', ' + typeof arr.valueOf() + '<br />');document.write(date.valueOf() + ', ' + typeof date + ', ' + typeof date.valueOf() + '<br />'); // 日期對(duì)象自己的valueOf方法document.write(fun.valueOf() + ', ' + typeof fun + ', ' + typeof fun.valueOf() + '<br /><br />');</script></body> </html>11.2. 效果圖
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的047_Object对象的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 043_对象构造器和原型
- 下一篇: 056_Object对象方法