您的位置:首页 > 其它

ECMA2015(ES6)简单入门-3-迭代器-生成器-定义类-__静态方法__继承

2017-04-14 20:00 417 查看
自定义迭代器


// {value: xx,done: true/false}

function chef(foods){

let i=0;

return {

next(){

let done =(i>=foods.length),

value= !done ?foods[i++]:undefined;

return {

value:value,

done:done

}

}

}

}

let qiao =chefs([“apple”,”apple1”]);

console.log(qiao.next());

//generators

// 定义生成器

function* chefs(){

yield “apple”;

yield “apple2”;

}

let wang=chefs();

console.log(wang.next());

console.log(wang.next());

console.log(wang.next());

console.log(“……………………………….”)



let wang_1=chefs_1([“apple”,”apple1”]);

console.log(wang_1.next());

console.log(wang_1.next());

console.log(wang_1.next());

//定义一个类

class chef_class{

//属性方法

// 基于这个类创建实例之后会

// 自动执行这个constructor这个方法

//可以把初始化的东西放到里面,让方法接受参数

constructor(food){

this.food=food;

};

cook(){

console.log(

this.food

)

};

}

let qiao_1=new chef_class(“egg”);

qiao_1.cook();

//类里面可以定义get和let get就是可以得到东西set可以设置东西

class chef_t{

//属性方法

// 基于这个类创建实例之后会

// 自动执行这个constructor这个方法

//可以把初始化的东西放到里面,让方法接受参数

constructor(food){

this.food=food;

this.dish = [];

};

get menu(){
return this.dish
};
set menu(dish){
this.dish.push(dish)
};


static cooks(food){

console.log(food)

}

}

let qiao_t=new chef_t();

console.log(qiao_t.menu=”egg1”);

console.log(qiao_t.menu=”egg2”);

console.log(qiao_t.menu);

// 类里面可以添加静态的方法static=>不需要实例化类就可以直接使用的方法

chef_t.cooks(“apple”);

//一个来可以继承其他类里面的东西

class Person {

constructor(name,birthday){

this.name=name;

this.birthday=birthday;

}

intro () {

return
${this.name},${this.birthday}


}

}

class anthor extends Person{

constructor(name,birthday){

super(name,birthday)

}

}

let qiao_x=new anthor(“qiaorui”,”1991-10”);

console.log(qiao_x.intro())
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息