टिप्पणी debiasej में लेख का उल्लेख नीचे लिखा डिजाइन पैटर्न के बारे में अधिक बताते हैं (इंटरफेस पर आधारित है, वर्ग):
http://loredanacirstea.github.io/es6-design-patterns/
डिजाइन पैटर्न पुस्तक जावास्क्रिप्ट में भी आपके लिए उपयोगी हो सकता है:
http://addyosmani.com/resources/essentialjsdesignpatterns/book/
डिजाइन पैटर्न = कक्षाएं + इंटरफ़ेस या एकाधिक वंशानुक्रम
ES6 जे एस में कारखाने पैटर्न का एक उदाहरण (चलाने के लिए: नोड 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);
पूरी तरह से? [दूर तक नहीं।] (Https://kangax.github.io/compat-table/es6/) – Bergi
जेएस अभी भी [बतख टाइपिंग] (https://en.wikipedia.org/wiki/Duck_typing) का उपयोग करता है। कोई स्थैतिक रूप से लागू "विधि अनुबंध" नहीं हैं। यदि आप गतिशील रूप से उनका परीक्षण करना चाहते हैं, तो आप आसानी से अपना इंटरफ़ेस चेकर लिख सकते हैं। – Bergi
ईएस 6 पर पहले से ही कुछ किताबें उपलब्ध हैं, उदा। [यह एक] (https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20&%20beyond/README.md#you-dont-know-js-es6--beyond) । यदि आप उनमें से एक को पढ़ते हैं तो आपको अब और आश्चर्य नहीं करना पड़ेगा कि ईएस 6 में कौन सी सुविधा उपलब्ध है और उपलब्ध नहीं है। सबसे बुरे मामले में, [spec देखें] (http://www.ecma-international.org/ecma-262/6.0/index.html)। शब्द "इंटरफ़ेस" 12 बार प्रकट होता है। कृपया प्रत्येक सुविधा के लिए कोई प्रश्न न बनाएं जो संभवतः एक भाषा हो सकती है। –