Trong comments debiasej viết đề cập dưới đây bài viết giải thích thêm về các mẫu thiết kế (dựa trên giao diện, lớp):
http://loredanacirstea.github.io/es6-design-patterns/
mẫu thiết kế cuốn sách trong javascript cũng có thể hữu ích cho bạn:
http://addyosmani.com/resources/essentialjsdesignpatterns/book/
Mẫu thiết kế = lớp + giao diện hoặc nhiều kế thừa
Ví dụ về mẫu nhà máy trong ES6 JS (để chạy: node example.js):
"use strict";
// Types.js - Constructors used behind the scenes
// A constructor for defining new cars
class Car {
constructor(options){
console.log("Creating Car...\n");
// some defaults
this.doors = options.doors || 4;
this.state = options.state || "brand new";
this.color = options.color || "silver";
}
}
// A constructor for defining new trucks
class Truck {
constructor(options){
console.log("Creating Truck...\n");
this.state = options.state || "used";
this.wheelSize = options.wheelSize || "large";
this.color = options.color || "blue";
}
}
// FactoryExample.js
// Define a skeleton vehicle factory
class VehicleFactory {}
// Define the prototypes and utilities for this factory
// Our default vehicleClass is Car
VehicleFactory.prototype.vehicleClass = Car;
// Our Factory method for creating new Vehicle instances
VehicleFactory.prototype.createVehicle = function (options) {
switch(options.vehicleType){
case "car":
this.vehicleClass = Car;
break;
case "truck":
this.vehicleClass = Truck;
break;
//defaults to VehicleFactory.prototype.vehicleClass (Car)
}
return new this.vehicleClass(options);
};
// Create an instance of our factory that makes cars
var carFactory = new VehicleFactory();
var car = carFactory.createVehicle({
vehicleType: "car",
color: "yellow",
doors: 6 });
// Test to confirm our car was created using the vehicleClass/prototype Car
// Outputs: true
console.log(car instanceof Car);
// Outputs: Car object of color "yellow", doors: 6 in a "brand new" state
console.log(car);
var movingTruck = carFactory.createVehicle({
vehicleType: "truck",
state: "like new",
color: "red",
wheelSize: "small" });
// Test to confirm our truck was created with the vehicleClass/prototype Truck
// Outputs: true
console.log(movingTruck instanceof Truck);
// Outputs: Truck object of color "red", a "like new" state
// and a "small" wheelSize
console.log(movingTruck);
Đầy đủ? [Không xa.] (Https://kangax.github.io/compat-table/es6/) – Bergi
JS vẫn sử dụng [gõ vịt] (https://en.wikipedia.org/wiki/Duck_typing). Không có "hợp đồng phương thức" được thực thi tĩnh ". Nếu bạn muốn kiểm tra chúng động, bạn có thể dễ dàng viết trình kiểm tra giao diện của riêng bạn. – Bergi
Hiện đã có một số sách có sẵn trên ES6, ví dụ: [this one] (https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20&%20beyond/README.md#you-dont-know-js-es6--beyond) . Nếu bạn đọc một trong số họ, bạn sẽ không phải băn khoăn về tính năng nào khác và không có sẵn trong ES6. Trong trường hợp xấu nhất, hãy xem [spec] (http://www.ecma-international.org/ecma-262/6.0/index.html). Thuật ngữ "giao diện" xuất hiện 12 lần. Vui lòng không tạo câu hỏi cho mọi tính năng mà ngôn ngữ có thể có. –