JS继承与Promise小计

2017-08-29  本文已影响0人  光影墨辰

//ES6实现继承

classPerson {

constructor(name, age) {

this.name= name;

this.age= age;

}

Show() {

return(this.name+' '+this.age);

}

}

classStudentextendsPerson {

constructor(name, age, School) {

super(name, age);

this.School= School;

}

Show() {

return(super.Show() +' '+this.School);

}

}

letme=newStudent("mochen","10","XD");

//ES5继承

functionjicheng(parent) {

functionF(){};

F.prototype= parent;

return newF();

}

//Promise实例

functionf(par) {

return newPromise((resolve, reject) => {

//异步操作

if(true)//如果判断条件为true,即异步执行成功

{

resolve(参数);

}else{

reject(参数);

}

})

}

letmyPromise=newf(par);

myPromise(par)

.then(()=> {})//此处为异步成功时你要执行的函数

.catch(() => {})//此处为异步失败你要执行的函数

上一篇下一篇

猜你喜欢

热点阅读