原型鏈
1蹂喻、普通對象和函數(shù)對象
在javascript中萬物皆對象惕医,對象分為普通對象和函數(shù)對象,可以通過typeof來判斷對象是哪一種類型的對象抓于。
1.1 普通對象
typeof obj === 'object'; // true
let?obj1 =?{};let?obj2 =?new?Object();let?obj3 =?new?Array();
console.log(typeof?obj1);?// object
console.log(typeof?obj2);?// object
console.log(typeof?obj3);?// object
1.2 函數(shù)對象
typeof obj === 'function'; // true
let?f1 =?new?Function('a',?'b',?'return a + b');let?f2?=?function?()?{};function?f3()?{};
console.log(typeof?f1);?// function
console.log(typeof?f2);?// function
console.log(typeof?f3);?// function
console.log(typeof?Function);?// function
console.log(typeof?Object);?// function
console.log(typeof?Array);?// function
console.log(Function.constructor ===?Function);?// true
console.log(Object.constructor ===?Function);?// true
console.log(Array.constructor ===?Function);?// true
注:?凡是通過new Function() 創(chuàng)建的對象都是函數(shù)對象,其他的都是普通對象浇借。f1,f2,f3,實(shí)際上也是要通過 new Function()的方式進(jìn)行創(chuàng)建捉撮。Function Object 也是通過 New Function()創(chuàng)建的。javascript大部分內(nèi)置對象的構(gòu)造器屬于函數(shù)對象逮刨。
Js中的對象可以分為三類:
1內(nèi)置對象:列入數(shù)組呕缭,函數(shù),日期
2宿主對象:即js解釋器所嵌入的宿主環(huán)境(比如瀏覽器)定義的修己,列如HTMLElement等
3自定義對象:及程序員用代碼定義的:對象的屬性分為兩類:
1自有屬性:直接在對象中定義的屬性
2繼承屬性:在對象的原型對象中定義是的屬性
創(chuàng)建對象的兩種方式:
1const obj = {a:1,b:2}
2 const obj = new Object();創(chuàng)建一個空對象恢总,和{}一樣
const obj = new Arary()創(chuàng)建一個空數(shù)組,和{}一樣
const obj = new Date()創(chuàng)建一個表示當(dāng)前時間的Date對象