//补充注释区块的代码
class m {
static instance(){
//返回实例
}
nameA(){
//返回调用的实例名称
}
static nameB(){
//返回调用的类名
}
}
class A extends m {}
class B extends m {}
A.instance().nameA() //'A'
B.nameB() //'B'
答案:
class m {
static instance(){
//返回实例
return new this
}
nameA(){
//返回调用的实例名称
return this.constructor.name
}
static nameB(){
//返回调用的类名
return this.name
}
}
考点:this,第一个函数里的this是类对象即A,所以new this 即new A;第二个函数里的this是new A得到的实例对象;第三个函数里的this即调用的类,即B
//链表,补充注释处代码
class Link{
static fromAry(ary){
let head=new this;
ary.reduce((pre,data)=>{
let node=new this;
pre.next=node
node.data=data
return node
},head)
return head
}
toAry(){
let ary=[]
for(let node=this.next;;node=node.next){
if(!node)break;
ary.push(node.data)
}
return ary
}
length(){
//链表长度
}
sort(){
//链表排序
}
}
let link=Link.fromAry([1,6,3,4,2])
link.sort() //1->2->3->4->6
答案:
class Link{
static fromAry(ary){
let head=new this;
ary.reduce((pre,data)=>{
let node=new this;
pre.next=node
node.data=data
return node
},head)
return head
}
toAry(){
let ary=[]
for(let node=this.next;;node=node.next){
if(!node)break;
ary.push(node.data)
}
return ary
}
length(){
//链表长度
return this.toAry().length
}
sort(){
//链表排序
let sortAry= this.toAry().sort((a,b)=>{return a-b})
return Link.fromAry(sortAry)
}
}