1.所有的對(duì)象都是通過 new function
創(chuàng)建的
let obj={}
本質(zhì)上是
let obj=new Object
2.所有的函數(shù)也是對(duì)象
3.因?yàn)楹瘮?shù)是對(duì)象因妙,所以可以有屬性
4.所有對(duì)象都是引用類型(保存和賦值的都是地址)
1.所有函數(shù)都有一個(gè)屬性:prototype
2.prototype默認(rèn)情況下是一個(gè)普通Object對(duì)象,只會(huì)具有constructor,其余繼承自object
3.默認(rèn)情況下prototype里面有一個(gè)constructor指向構(gòu)造函數(shù)
1.所有對(duì)象都有一個(gè)屬性:__proto__
隱式原型
2.默認(rèn)情況下徽诲,隱式原型指向創(chuàng)建該對(duì)象的函數(shù)的原型
function test(){
return []
}
let obj=new test()
console.log(obj.__proto__===Array.prototype); true
function test(){
}
let obj=new test()
console.log(obj.__proto__===test.prototype); true
isPrototypeOf() 方法檢查一個(gè)對(duì)象是否存在于另一個(gè)對(duì)象的原型鏈上
function Pron(){}
let test=new Pron
console.log(Pron.prototype.isPrototypeOf(test));true
Object.getPrototypeOf() 方法返回指定對(duì)象的原型(內(nèi)部[[Prototype]]屬性的值
console.log(Object.getPrototypeOf(Object));
Object.setPrototypeOf()** 方法設(shè)置一個(gè)指定的對(duì)象的原型(即森瘪,內(nèi)部 [[Prototype]]
屬性)到另一個(gè)對(duì)象或 null
Object.setPrototypeOf(obj, prototype)
// obj 要設(shè)置其原型的對(duì)象
//prototype 要設(shè)置其原型的對(duì)象
//為避免導(dǎo)致性能下降牡属,建議使用Object.create() 方法用于創(chuàng)建一個(gè)新對(duì)象,使用現(xiàn)有的對(duì)象來(lái)作為新創(chuàng)建對(duì)象的原型
const test={
name:'hello'
}
const test01=Object.create(test)
test01.age=20
console.log(test01.name);
console.log(test01.age);
console.log(test01.__proto__==test);
hasOwnProperty確定屬性在實(shí)例上還是原型對(duì)象上
function test(){
}
test.prototype.name='hello'
let test01=new test
console.log(test01.hasOwnProperty('name'));//false
test01.name='nihao'
console.log(test01.hasOwnProperty('name'));//true
delete test01.name
console.log(test01.hasOwnProperty('name'));//false
Object.getOwnPropertyDescriptor() 方法返回指定對(duì)象上一個(gè)自有屬性對(duì)應(yīng)的屬性描述符扼睬。(自有屬性指的是直接賦予該對(duì)象的屬性逮栅,不需要從原型鏈上進(jìn)行查找的屬性)
Object.getOwnPropertyDescriptor(obj, prop)
obj 需要查找的目標(biāo)對(duì)象
prop 目標(biāo)對(duì)象內(nèi)屬性名稱
in操作符會(huì)在可以訪問指定屬性時(shí)返回true,無(wú)論屬性是在實(shí)例上還是原型上
function test(){
}
test.prototype.name='hello'
const test01=new test
const test02=new test
test02.name='nihao'
console.log('name' in test01);//原型上 true
console.log('name' in test02);//實(shí)例上 true
//判斷屬性是否在原型上
function has(obj,name){
return !obj.hasOwnProperty(name)&&(name in obj)
}
console.log(has(test01,'name'));//true
console.log(has(test02,'name'));//false
Object.keys可獲取對(duì)象上所有可枚舉的屬性
function test(){
}
test.prototype.name='name'
test.prototype.age=20
console.log(Object.keys(test.prototype));//[ 'name', 'age' ]
const test01=new test
test01.password=20
console.log(Object.keys(test01));//[ 'password' ]
如果想要列出所有實(shí)例屬性窗宇,無(wú)論是否可以被枚舉
console.log(Object.getOwnPropertyNames(test.prototype));//[ 'constructor', 'name', 'age' ]
對(duì)象迭代
const test={
name:'name',
age:20,
password:123,
say:function(){}
}
console.log(Object.values(test));//[ 'name', 20, 123, [Function: say] ]
console.log(Object.entries(test));//[[ 'name', 'name' ], [ 'age', 20 ], [ 'password', 123 ],[ 'say', [Function: say] ]]
console.log(Object.entries(test)[3][1]==test.say)//true
Object.defineProperty() 方法會(huì)直接在一個(gè)對(duì)象上定義一個(gè)新屬性措伐,或者修改一個(gè)對(duì)象的現(xiàn)有屬性
function test(){
}
test.prototype={
name:'name',
age:20
}
//恢復(fù)constructor屬性
Object.defineProperty(test.prototype,'constructor',{
enumerable:false,//不可枚舉
value:test
})