2015-09-17 14 views
71

ES6 hoàn toàn có sẵn trong Nút 4. Tôi đã tự hỏi liệu nó có bao gồm khái niệm giao diện để xác định hợp đồng phương thức như trong MyClass implements MyInterface hay không.Có cách nào để tạo giao diện trong ES6/Nút 4 không?

Tôi không thể tìm thấy nhiều với Googling của mình, nhưng có thể có một mẹo hay giải pháp hay.

+2

Đầy đủ? [Không xa.] (Https://kangax.github.io/compat-table/es6/) – Bergi

+1

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

+0

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ó. –

Trả lời

52

Giao diện không phải là một phần của ES6 mà là các lớp.

Nếu bạn thực sự cần chúng, bạn nên xem TypeScript để hỗ trợ chúng.

+0

"chúng" là giao diện. FWIW Bạn có thể cần phải xem xét cẩn thận liên kết cho trình phát được cung cấp ở trên. Không chính xác như tôi mong đợi, nhưng gần gũi. – Berniev

9

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); 
+19

Vậy đâu là một giao diện mà tôi có thể sáng tác với người khác? –

+0

Một số giải thích sâu hơn là tại trang web này: https://www.sitepoint.com/object-oriented-javascript-deep-dive-es6-classes/ – 42n4

+2

Có một bản cập nhật tuyệt vời các mẫu ES5 cho ES6 tại trang này: http: //loredanacirstea.github.io/es6-design-patterns/ – debiasej

Các vấn đề liên quan