自定义迭代器: {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())