自有屬性:對(duì)象實(shí)例私有的屬性,只有該對(duì)象實(shí)例可用
共有屬性:對(duì)象實(shí)例共有的屬性十电,所有對(duì)象實(shí)例都可用
要判斷對(duì)象實(shí)例的自有屬性、共有屬性抑堡;首先看看JS給我們提供的兩個(gè)方法:
1摆出、判斷是否是對(duì)象實(shí)例的屬性
"屬性名"? in? 對(duì)象實(shí)例
2、判斷是否是對(duì)象實(shí)例的自有屬性
對(duì)象實(shí)例.hasOwnProperty('屬性名')
定義一個(gè)對(duì)象實(shí)例:
varobj = {
? ? name: '小馬扎',
? ? age: 18};?
Object.prototype.car ='筋斗云';// 在Object類中定義car屬性
1首妖、判斷是否是對(duì)象實(shí)例的屬性
console.log("car" in obj);// true
console.log("fly" in obj);// false
2偎漫、判斷是否是對(duì)象實(shí)例私有的屬性
console.log(obj.hasOwnProperty('name'));// true?
console.log(obj.hasOwnProperty('car'));// false
3、判斷是否是對(duì)象實(shí)例私有的屬性
console.log(!obj.hasOwnProperty("car") &&"car" in obj)// true
console.log(!obj.hasOwnProperty("name") &&"name" in obj)// false
轉(zhuǎn)載自:https://www.cnblogs.com/minigrasshopper/p/8066997.html有缆,用作記錄學(xué)習(xí)