
批改狀態(tài):合格
老師批語:完成的很好, 繼續(xù)加油
/*
作業(yè)內(nèi)容:
1. 實例演示class類與extends,super等的用法
2. 實例演示字符串,數(shù)組常用API (至少5個以上)
*/
// 一、實例演示class類與extends,super等的用法
// 父類
let Small_Head_Dad = class {
// 構(gòu)造函數(shù):聲明屬性
constructor(name) {
this.name = name;
// 技能
this.sing = '會高歌一曲';
this.cook = '一桌滿漢全席';
}
// 方法
say(){
return this.name + this.sing
}
// 方法
dinner(){
return this.name + this.cook
}
}
// 創(chuàng)建新對象
let datou = new Small_Head_Dad('小頭爸爸')
// 輸出父親會唱歌
console.log(datou.say())
// 大頭兒子繼承唱歌 繼承
class Big_Head_Son extends Small_Head_Dad {
constructor(name,sing,status) {
// super 調(diào)用父類成員
super(name,sing,status);
// 子類擴(kuò)展的屬性
this.status = '上學(xué)'
}
// 子類方法
school(){
return this.name + '還在'+this.status
}
// 子類方法
skill(){
return this.name + '會'+this.sing
}
}
let son = new Big_Head_Son('大頭兒子')
// 兒子繼承了父親的唱歌方法
console.log(son.say())
// 子類方法
console.log('子類方法1'+son.school())
console.log('子類方法2'+son.skill())
// 二、 實例演示字符串,數(shù)組常用API (至少5個以上)
// replace()替換
let arr = '今天天氣怎么樣?';
console.log(arr.replace('怎么樣?','晴朗!')) // 今天天氣晴朗!
// substring() 提取字符
console.log(arr.substring(0,4)) // 今天天氣
// split() 字符串 -> 數(shù)組
console.log(arr.split('怎')) // [ '今天天氣', '么樣?' ]
// toString() 將數(shù)組轉(zhuǎn)為字符串
let arr2 = ['1','3','5','7']
console.log(arr2.toString()) // ['1','3','5','7'] => 1,3,5,7
// reverse() 翻轉(zhuǎn)數(shù)組
console.log(arr2.reverse()) // [ '7', '5', '3', '1' ]
// push() 在末尾添加元素
console.log(arr2.push('新增')) // 返回值是數(shù)組長度
console.log(arr2) // [ '7', '5', '3', '1', '新增' ]
// pop() 刪除數(shù)組末尾的一個元素
console.log(arr2.pop()) // 返回值是刪除的那個元素
console.log(arr2) // [ '7', '5', '3', '1' ]
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號