ES6 / নোড 4 এ ইন্টারফেস তৈরি করার কোনও উপায় আছে?


110

ES6 নোড 4 এ সম্পূর্ণরূপে উপলভ্য I আমি ভাবছিলাম যে এটিতে পদ্ধতির চুক্তিগুলি সংজ্ঞায়িত করার জন্য ইন্টারফেসের ধারণা অন্তর্ভুক্ত কিনা MyClass implements MyInterface

আমি আমার গুগলিংয়ের সাথে খুব বেশি কিছু খুঁজে পাচ্ছি না তবে সম্ভবত এটি একটি দুর্দান্ত কৌশল বা কার্যকরী উপলব্ধ।



1
জেএস এখনও হাঁসের টাইপিং ব্যবহার করে । কোনও স্ট্যাটিক্যালি প্রয়োগ করা "পদ্ধতির চুক্তি" নেই। আপনি যদি এগুলি গতিশীলভাবে পরীক্ষা করতে চান তবে আপনি সহজেই নিজের ইন্টারফেস চেকার লিখতে পারেন।
বার্গি

26
পার্টিতে দেরীতে, তবে অসম্মতিজনক প্রশ্নটি অফ-বিষয় is কোনও প্রত্যাশিত বৈশিষ্ট্য উপস্থিত থাকলে ওপি নিশ্চিতকরণ চায়। ক্লাসগুলির জন্য নতুন, সরলিকৃত, সিনট্যাক্সটি দীর্ঘ সময়সীমার এবং সম্ভবত এটি ব্যাপকভাবে ব্যবহৃত হবে। তবে ইন্টারফেসগুলি খুব ভাল কারণে অন্যান্য ভাষায় সাধারণ। আমিও অবাক হয়েছিলাম, এবং হতাশ হয়েছি ইন্টারফেসগুলি ES2015 এর অংশ নয় learn প্রদত্ত যে এটি সম্ভবত একটি সাধারণ আবিষ্কার, আইএমএইচও এটির প্রস্তাবিত কর্মসংস্থান আছে কিনা তা জিজ্ঞাসা করা অযৌক্তিক নয়।

9
পৃথিবীতে কীভাবে এই বিষয় বন্ধ? ইন্টারফেসগুলি কোনও পণ্য নয় প্রোগ্রামিং কৌশল। প্রশ্নটি বৈধ এবং ECMA স্ক্রিপ্ট the প্রকাশের সাথে জাভা আনতে ক্লাস সংজ্ঞা পছন্দ করে with আমি মনে করি যে এই বিষয়টির সমাপ্তি বোঝার অভাব এবং কীভাবে স্ট্যাকের উপর দিয়ে পয়েন্ট সিস্টেমটি সাফল্যের সাথে সামঞ্জস্য করে না তা প্রমাণ করে।
অ্যান্ড্রু এস

4
আক্ষরিক অর্থে কোনও প্রশ্নে ওপ (আমাদের জিজ্ঞাসা করবেন না) এই প্রশ্নের যে কোনও একটিতে কোনও বই, সরঞ্জাম, সফ্টওয়্যার লাইব্রেরি, টিউটোরিয়াল বা অফ-সাইট রিসোর্সের প্রস্তাব বা সন্ধান করার জন্য
লিয়াম

উত্তর:


90

ইন্টারফেসগুলি ES6 এর অংশ নয় তবে ক্লাসগুলি।

আপনার যদি সত্যিই তাদের প্রয়োজন হয় তবে আপনার টাইপস্ক্রিপ্টের দিকে নজর দেওয়া উচিত যা তাদের সমর্থন করে


1
"তাদের" ইন্টারফেস হচ্ছে। FWIW আপনার উপরের সরবরাহকৃত ট্রান্সপ্লেলারটির লিঙ্কটি সাবধানতার সাথে বিবেচনা করতে হতে পারে। আমি যেমনটা প্রত্যাশা করছিলাম ঠিক তেমনটি নয়, তবে কাছেও।

একটি নোট: যতক্ষণ না আমি টাইপসক্রিপ্টের বিশুদ্ধ ইন্টারফেসটি কিছুই প্রতিস্থাপন করে না। কেবলমাত্র আপনি যদি সেগুলি ব্যবহার করেন তবে প্রতিস্থাপন কোডটির একটি নির্দিষ্ট যুক্তি রয়েছে।
ড্যানিয়েল ড্যানিয়েলেকি

9

মন্তব্যে দেবিজেজ লিখেছেন নীচের নিবন্ধটি ডিজাইনের ধরণ সম্পর্কে আরও ব্যাখ্যা করেছে (ইন্টারফেস, ক্লাসের উপর ভিত্তি করে):

http://loredanacirstea.github.io/es6-design-patterns/

জাভাস্ক্রিপ্টে নকশার প্যাটার্ন বইটি আপনার জন্যও কার্যকর হতে পারে:

http://addyosmani.com/resources/essentialjsdesignpatterns/book/

নকশার ধরণ = শ্রেণি + ইন্টারফেস বা একাধিক উত্তরাধিকার

ES6 জেএসে কারখানার প্যাটার্নের একটি উদাহরণ (চালানোর জন্য: নোড উদাহরণ.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 );

34
সুতরাং এখানে একটি ইন্টারফেস যেখানে আমি অন্যদের সাথে রচনা করতে পারি?
দিমিত্রি জইতসেভ

কিছু গভীর ব্যাখ্যা এই সাইটে রয়েছে: sitepoint.com/object-oriented-
জাভাস্ক্রিপ্ট- প্রদীপ

2
: এই সাইট এ ES6 করার ES5 রীতির একটি মহান আপডেট হয় loredanacirstea.github.io/es6-design-patterns
debiasej

8

ইসিএমএ একটি 'শ্রেণি-মুক্ত' ভাষা বলে দেওয়া হয়েছে, শাস্ত্রীয় রচনাটি প্রয়োগ করা - আমার দৃষ্টিতে - অনেক কিছু বোঝায় না। বিপদটি হ'ল, এর ফলে আপনি কার্যকরভাবে ভাষাটি পুনরায় ইঞ্জিনিয়ার করার চেষ্টা করছেন (এবং যদি কেউ যদি এটি সম্পর্কে দৃ strongly় মনে করেন, তবে সর্বোত্তম সামগ্রিক সমাধান যেমন পূর্বোক্ত টাইপস্ক্রিপ্ট যেমন চাকাটিকে পুনরায় উদ্বেগ প্রশমিত করতে পারে)

এখন এটি বলা বাহুল্য নয় যে সমষ্টি ওল্ড জেএসে যদিও রচনাটি প্রশ্নটির বাইরে। কিছুক্ষণ আগে আমি এ নিয়ে গবেষণা করেছি। প্রোটোটাইপাল দৃষ্টান্তের মধ্যে রচনাটি পরিচালনা করার জন্য আমি যে শক্তিশালী প্রার্থী দেখেছি তা হ'ল স্ট্যাম্পিট , যা আমি এখন বিস্তৃত বিভিন্ন প্রকল্পে ব্যবহার করি। এবং, গুরুত্বপূর্ণভাবে, এটি একটি ভাল স্পষ্ট স্পেসিফিকেশন মেনে চলে।

স্ট্যাম্পের উপর আরও তথ্যের এখানে


1
আমি আমার পোস্টের সাথে এমনকি -1 দিয়ে দাঁড়িয়েছি। দুঃখের বিষয়, এটি কখনও কখনও এসও এর গণতন্ত্র। আমি আশা করি যে কেউ লিঙ্কগুলি দরকারী বলে মনে করেন। স্ট্যাম্পিট আপনার সময়ের জন্য মূল্যবান।
জে এডওয়ার্ডস

-1 চূড়ান্ত রায় নয়। আপনার পোস্টটি শেষ হতে পারে + 100 / -1। তবে আমি এখনও এটি অস্পষ্ট মনে করি। জেএস আর "ক্লাস-মুক্ত" নয়। আমি সন্দেহ করি যে "ধ্রুপদী সংমিশ্রণ "টি আপনি কী বোঝাতে চেয়েছেন তা বোঝাতে বেশিরভাগই বুঝতে পারবেন না: উত্তরাধিকার। (সম্পূর্ণ উত্তরাধিকার বনাম রচনা পবিত্র যুদ্ধ বিবেচনা করুন।) "সমতল ওল্ড জেএস" কী তাও পরিষ্কার নয়। ES5? আরও ভার্বোস সিনট্যাক্স সত্ত্বেও, এটি এমন কৌশলগুলিকে সমর্থন করে যা এখন আরও বেশি বিস্তৃত, যেমন "সত্য" মিশ্রণগুলি । স্ট্যাম্পগুলি আকর্ষণীয় দেখায়, মিক্স-ইনগুলির তুলনায় তাদের সুবিধা কী কী?
4 ᆺ ᆼ

ক্লাস কীওয়ার্ডটি সিনট্যাকটিক চিনি। জেএস - ইএস ^ 6 বা অন্যথায় - কোনও শ্রেণি ভাষা নয়। এটি কেবল ES5 এ traditionalতিহ্যবাহী ফাংশন নির্মাণকারী পদ্ধতির সজ্জিত করে। "সাধারণ পুরানো জেএস" ইএস এর জেএস প্রয়োগের যে কোনও একটিটিকে আনন্দের সাথে সংজ্ঞায়িত করে। সত্যি বলতে গেলে, আমি আশা করি যে ভাষাটি কোরা ভাষায় ক্লাসের ধারণাটি আরও জড়িত করার সিদ্ধান্ত নেওয়া না হয়েছিল। স্ট্যাম্পগুলি জেএসের শক্তি আইএমএইচওর প্রতিফলন করে। stampit.js.org ক্লাস থেকে পার্থক্যগুলির একটি ভাল পালটা দেয়। শেষ পর্যন্ত, এটি আরও বাস্তববাদী পদ্ধতি।
জে এডওয়ার্ডস

1
তবে তারপরে "শ্রেণিবদ্ধ ভাষা" কী? সি ++? classশুধুমাত্র একটি প্রতিশব্দ struct। স্মলটকের মতো সত্যই ক্লাসিক ভাষা? এটি প্রোটোটাইপগুলি এমনকি দৃষ্টান্তগুলির গতিশীল
প্রসারকেও মঞ্জুরি দেয়

এটি একটি যুক্তিসঙ্গত বিষয়। আমি একটি শ্রেণিবদ্ধ ভাষাটি এমন ভাষা হিসাবে সংজ্ঞায়িত করব যা অন্তর্গতভাবে OOP। এমডিএন থেকে: "জাভাস্ক্রিপ্ট হ'ল একটি প্রোটোটাইপ-ভিত্তিক, বহু-দৃষ্টান্ত, গতিশীল ভাষা, সমর্থনকারী অবজেক্ট-ভিত্তিক, অত্যাবশ্যকীয় এবং ঘোষণামূলক (যেমন কার্যকরী প্রোগ্রামিং) শৈলী" " google.com/url?sa=t&source=web&rct=j&url=https://…
জে এডওয়ার্ডস

6

সমস্যাটির জন্য এটিই আমার সমাধান। একের সাথে অন্য ইন্টারফেসকে ওভাররাইড করে আপনি একাধিক ইন্টারফেসকে 'প্রয়োগ' করতে পারেন।

class MyInterface {
    // Declare your JS doc in the Interface to make it acceable while writing the Class and for later inheritance
    /**
     * Gives the sum of the given Numbers
     * @param {Number} a The first Number
     * @param {Number} b The second Number
     * @return {Number} The sum of the Numbers
     */
    sum(a, b) { this._WARNING('sum(a, b)'); }


    // delcare a warning generator to notice if a method of the interface is not overridden
    // Needs the function name of the Interface method or any String that gives you a hint ;)
    _WARNING(fName='unknown method') {
        console.warn('WARNING! Function "'+fName+'" is not overridden in '+this.constructor.name);
    }
}

class MultipleInterfaces extends MyInterface {
    // this is used for "implement" multiple Interfaces at once
    /**
     * Gives the square of the given Number
     * @param {Number} a The Number
     * @return {Number} The square of the Numbers
     */
    square(a) { this._WARNING('square(a)'); }
}

class MyCorrectUsedClass extends MyInterface {
    // You can easy use the JS doc declared in the interface
    /** @inheritdoc */
    sum(a, b) {
        return a+b;
    }
}
class MyIncorrectUsedClass extends MyInterface {
    // not overriding the method sum(a, b)
}

class MyMultipleInterfacesClass extends MultipleInterfaces {
    // nothing overriden to show, that it still works
}


let working = new MyCorrectUsedClass();

let notWorking = new MyIncorrectUsedClass();

let multipleInterfacesInstance = new MyMultipleInterfacesClass();

// TEST IT

console.log('working.sum(1, 2) =', working.sum(1, 2));
// output: 'working.sum(1, 2) = 3'

console.log('notWorking.sum(1, 2) =', notWorking.sum(1, 2));
// output: 'notWorking.sum(1, 2) = undefined'
// but also sends a warn to the console with 'WARNING! Function "sum(a, b)" is not overridden in MyIncorrectUsedClass'

console.log('multipleInterfacesInstance.sum(1, 2) =', multipleInterfacesInstance.sum(1, 2));
// output: 'multipleInterfacesInstance.sum(1, 2) = undefined'
// console warn: 'WARNING! Function "sum(a, b)" is not overridden in MyMultipleInterfacesClass'

console.log('multipleInterfacesInstance.square(2) =', multipleInterfacesInstance.square(2));
// output: 'multipleInterfacesInstance.square(2) = undefined'
// console warn: 'WARNING! Function "square(a)" is not overridden in MyMultipleInterfacesClass'

সম্পাদনা করুন:

আমি কোডটি উন্নত করেছি যাতে আপনি এখন কেবল প্রসারিত (বেসক্লাস, ইন্টারফেস 1, ইন্টারফেস 2, ...) ব্যবহার করতে পারেন।

/**
* Implements any number of interfaces to a given class.
* @param cls The class you want to use
* @param interfaces Any amount of interfaces separated by comma
* @return The class cls exteded with all methods of all implemented interfaces
*/
function implement(cls, ...interfaces) {
    let clsPrototype = Object.getPrototypeOf(cls).prototype;
    for (let i = 0; i < interfaces.length; i++) {
        let proto = interfaces[i].prototype;
        for (let methodName of Object.getOwnPropertyNames(proto)) {
            if (methodName!== 'constructor')
                if (typeof proto[methodName] === 'function')
                    if (!clsPrototype[methodName]) {
                        console.warn('WARNING! "'+methodName+'" of Interface "'+interfaces[i].name+'" is not declared in class "'+cls.name+'"');
                        clsPrototype[methodName] = proto[methodName];
                    }
        }
    }
    return cls;
}

// Basic Interface to warn, whenever an not overridden method is used
class MyBaseInterface {
    // declare a warning generator to notice if a method of the interface is not overridden
    // Needs the function name of the Interface method or any String that gives you a hint ;)
    _WARNING(fName='unknown method') {
        console.warn('WARNING! Function "'+fName+'" is not overridden in '+this.constructor.name);
    }
}


// create a custom class
/* This is the simplest example but you could also use
*
*   class MyCustomClass1 extends implement(MyBaseInterface) {
*       foo() {return 66;}
*   }
*
*/
class MyCustomClass1 extends MyBaseInterface {
    foo() {return 66;}
}

// create a custom interface
class MyCustomInterface1 {
     // Declare your JS doc in the Interface to make it acceable while writing the Class and for later inheritance

    /**
     * Gives the sum of the given Numbers
     * @param {Number} a The first Number
     * @param {Number} b The second Number
     * @return {Number} The sum of the Numbers
     */
    sum(a, b) { this._WARNING('sum(a, b)'); }
}

// and another custom interface
class MyCustomInterface2 {
    /**
     * Gives the square of the given Number
     * @param {Number} a The Number
     * @return {Number} The square of the Numbers
     */
    square(a) { this._WARNING('square(a)'); }
}

// Extend your custom class even more and implement the custom interfaces
class AllInterfacesImplemented extends implement(MyCustomClass1, MyCustomInterface1, MyCustomInterface2) {
    /**
    * @inheritdoc
    */
    sum(a, b) { return a+b; }

    /**
    * Multiplies two Numbers
    * @param {Number} a The first Number
    * @param {Number} b The second Number
    * @return {Number}
    */
    multiply(a, b) {return a*b;}
}


// TEST IT

let x = new AllInterfacesImplemented();

console.log("x.foo() =", x.foo());
//output: 'x.foo() = 66'

console.log("x.square(2) =", x.square(2));
// output: 'x.square(2) = undefined
// console warn: 'WARNING! Function "square(a)" is not overridden in AllInterfacesImplemented'

console.log("x.sum(1, 2) =", x.sum(1, 2));
// output: 'x.sum(1, 2) = 3'

console.log("x.multiply(4, 5) =", x.multiply(4, 5));
// output: 'x.multiply(4, 5) = 20'

0

ইন্টারফেস অনুকরণ করতে পারে এমন প্যাকেজ রয়েছে।

আপনি es6- ইন্টারফেস ব্যবহার করতে পারেন


2
আপনার উত্তরের সমস্যাটি হ'ল তিনি এটি করার জন্য কোনও "সরঞ্জাম" চেয়েছিলেন নি। তবে কীভাবে এটি করা হয়েছিল তার উত্তরটি আরও সঠিক হতে পারে যা বানান যে ফর্মটি এটি কীভাবে করেছে।
জিয়ানফ্রান্সেস্কো অরেচিয়া
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.