Skip to content

Latest commit

 

History

History
68 lines (57 loc) · 1.24 KB

classes1.md

File metadata and controls

68 lines (57 loc) · 1.24 KB

使用方法链接

Bad logo

	class Car {
	  constructor(make, model, color) {
	    this.make = make;
	    this.model = model;
	    this.color = color;
	  }
	
	  setMake(make) {
	    this.make = make;
	  }
	
	  setModel(model) {
	    this.model = model;
	  }
	
	  setColor(color) {
	    this.color = color;
	  }
	
	  save() {
	    console.log(this.make, this.model, this.color);
	  }
	}
	
	const car = new Car("Ford", "F-150", "red");
	car.setColor("pink");
	car.save();

Good logo

	class Car {
	  constructor(make, model, color) {
	    this.make = make;
	    this.model = model;
	    this.color = color;
	  }
	
	  setMake(make) {
	    this.make = make;
	    // 注意:返回这个用于链接
	    return this;
	  }
	
	  setModel(model) {
	    this.model = model;
	    // 注意:返回这个用于链接
	    return this;
	  }
	
	  setColor(color) {
	    this.color = color;
	    // 注意:返回这个用于链接
	    return this;
	  }
	
	  save() {
	    console.log(this.make, this.model, this.color);
	    // 注意:返回这个用于链接
	    return this;
	  }
	}
	
	const car = new Car("Ford", "F-150", "red").setColor("pink").save();