寫出一個(gè)構(gòu)造函數(shù) Animal
輸入為空
輸出為一個(gè)新對(duì)象羹膳,該對(duì)象的共有屬性為 {行動(dòng): function(){}},沒有自有屬性
再寫出一個(gè)構(gòu)造函數(shù) Human
Human 繼承 Animal
輸入為一個(gè)對(duì)象肠套,如 {name: 'Frank', birthday: '2000-10-10'}
輸出為一個(gè)新對(duì)象痕貌,該對(duì)象自有的屬性有 name 和 birthday,共有的屬性有物種(人類)糠排、行動(dòng)和使用工具
在寫出一個(gè)構(gòu)造函數(shù) Asian
Asian 繼承 Human
輸入為一個(gè)對(duì)象舵稠,如 {city: '北京', name: 'Frank', birthday: '2000-10-10' }
輸出為一個(gè)新對(duì)象,改對(duì)象自有的屬性有 name city 和 bitrhday入宦,共有的屬性有物種哺徊、行動(dòng)和使用工具和膚色
既
最后一個(gè)新對(duì)象是 Asian 構(gòu)造出來(lái)的
Asian 繼承 Human,Human 繼承 Animal
答案:
function Animal() {
}
Animal.prototype.行動(dòng) = function () { }
var animal = new Animal()
function Human(options) {
this.name = options.name
this.birthday = options.birthday
}
Human.prototype = Object.create(Animal.prototype)
Human.prototype.物種 = '人類'
Human.prototype.使用工具 = function () { }
var human = new Human({ name: 'frank', birthday: '2018-09-28' })
function Asian(options) {
Human.call(this, options)
this.city = options.city
}
Asian.prototype = Object.create(Human.prototype)
Asian.prototype.膚色 = '黃色'
var asian = new Asian({ city: 'beijing', name: 'frank', birthday: '2018-09-28' })
console.dir(animal)
console.dir(human)
console.dir(asian)
寫出一個(gè)構(gòu)造函數(shù) Animal
輸入為空
輸出為一個(gè)新對(duì)象落追,該對(duì)象的共有屬性為 {行動(dòng): function(){}},沒有自有屬性
再寫出一個(gè)構(gòu)造函數(shù) Human
Human 繼承 Animal
輸入為一個(gè)對(duì)象涯肩,如 {name: 'Frank', birthday: '2000-10-10'}
輸出為一個(gè)新對(duì)象轿钠,該對(duì)象自有的屬性有 name巢钓、物種和 birthday,共有的屬性有行動(dòng)和使用工具 (由于 class 的語(yǔ)法問題疗垛,所以物種只能勉為其難作為一個(gè)自有屬性症汹,本來(lái)應(yīng)該是共有屬性的)
再寫出一個(gè)構(gòu)造函數(shù) Asian
Asian 繼承 Human
輸入為一個(gè)對(duì)象,如 {city: '北京', name: 'Frank', birthday: '2000-10-10' }
輸出為一個(gè)新對(duì)象贷腕,改對(duì)象自有的屬性有 name city 物種 膚色和 bitrhday背镇,共有的屬性有行動(dòng)和使用工具
既
最后一個(gè)新對(duì)象是 Asian 構(gòu)造出來(lái)的
Asian 繼承 Human,Human 繼承 Animal
注意泽裳,要使用 class 關(guān)鍵字
答案:
class Animal {
行動(dòng)() { }
}
class Human extends Animal {
constructor(options) {
super(options)
this.name = options.name
this.birthday = options.birthday
this.物種 = '人類'
}
使用工具() { }
}
class Asian extends Human {
constructor(options) {
super(options)
this.city = options.city
this.膚色 = options.膚色
}
}
console.dir(Animal)
console.dir(Human)
console.dir(Asian)