js中原型和原型鏈

2021-10-09 14:22:24 字數 1395 閱讀 6280

let hd=new object();

object.prototype.show=function()

function getname()

console.dir(getname)

console.log(getname.prototype.__proto__==getname.__proto__.__proto__)//true

console.log(getname.prototype.__proto__==object.prototype)//true

let str=;

let a=

console.log(str.__proto__==object.prototype);//true

console.log(object.setprototypeof(str,a));//設定str原型為a

console.log(object.getprototypeof(str)==a);//true

function user(name)

let j=new user('yang')

console.log(j.__proto__==user.prototype)//true

console.log(j)

//isprototypeof 判斷原型

let ab={};

let b={};

object.setprototypeof(ab,b);//設定ab的原型b

console.log(b.isprototypeof(ab));//true 判斷b是不是ab的原型

//屬性的檢測

let a1=

let b_name=

object.setprototypeof(a1,b_name);//

//檢測name是不是a1的屬性 返回布林值

console.log('name' in a1) //繼承 true

for(let key in a1)

}//求最大值

let arr=[11,2,31,14,5,33,20,4,90];

let maxarr=

object.setprototypeof(arr,

})let newarr=

}console.log(arr.max.call(null,object.values(newarr.lessons)));//99

//優化之後的code

console.log(math.max.call(null,...maxarr.data));//12

儲存取消

儲存取消

JavaScript中原型和原型鏈

原型 prototype 為其他物件提供共享屬性的物件。每個函式都有乙個原型 prototype 屬性,這個屬性是乙個指標,指向乙個物件,這個物件包含特定例項共享的一些屬性和方法。以例服人 這個例子說明了原型物件是共享的,並且是乙個指標,並且物件的例項中也有指向prototype指向物件的指標。fu...

js中原型鏈的理解

原型鏈是理解js物件導向很重要的一點,這裡主要涉及到兩個點,一是proto,二是prototype,舉個例子吧 例如 我用function建立乙個person類,然後用new person 建立乙個物件的例項,假如叫做pl,在person類的原型prototype新增乙個方法,例如 play方法,那...

JS原型和原型鏈

建立建構函式 function word words word.prototype 建立例項 var w new word hello world w.print function w.print hello world w.alert hello world function.prototype....