title: 面向?qū)ο?八)繼承___ 04借用構(gòu)造函數(shù)實(shí)現(xiàn)繼承 # 文章頁(yè)面上的顯示名
date: # 文章生成時(shí)間,一般不改
categories: # 文章分類目錄,可省略
- 面向?qū)ο?br>
tags: # 文章標(biāo)簽,可省略
- 面向?qū)ο?br>
- 基于原型面向?qū)ο蟮睦^承 # 個(gè)數(shù)不限,單個(gè)可直接跟在 tags 后面
借用構(gòu)造函數(shù)實(shí)現(xiàn)繼承
<script>
function Person(name,age){
this.name = name;
this.age = age;
};
function Boy(bookName,name,age){
this.bookName = bookName;
//Person.call(this,"悟空",600); //借用構(gòu)造函數(shù)
Person.call(this,name,age);
}
//創(chuàng)建對(duì)象
var boy = new Boy("水煮三國(guó)","悟空",600);
var boy02 = new Boy("大話西游","云風(fēng)",40);
console.log(boy);
console.log(boy02);
</script>
核心代碼:Person.call(this,name,age);
在子構(gòu)造函數(shù)中調(diào)用父構(gòu)造函數(shù),由this來執(zhí)行Person函數(shù)
思考:調(diào)用Person構(gòu)造函數(shù),會(huì)產(chǎn)生對(duì)象嗎?
答案:不會(huì)。以普通方式調(diào)用Person函數(shù),和使用new關(guān)鍵字調(diào)用構(gòu)造函數(shù)是不一樣的