I
I
Isaac Clark2014-10-29 19:42:06
JavaScript
Isaac Clark, 2014-10-29 19:42:06

How to create an interface in JavaScript?

Hello, please help and explain.
I understand that there are no interfaces in js, since there are no classes in this language, and everything is built on objects and prototypes, which in turn cannot be strongly typed (please correct me if I'm wrong).
But still, be so kind as to explain what an interface is? Preferably using javascript as an example (I mean how it can be emulated purely for theoretical purposes in this language).
And is it true that the closest emulation of an interface in JavaScript is abstract classes?
Thanks for your help and your time.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey, 2014-10-29
Protko @Fesor

JS is a dynamic language with duck typing. Actually, like in Ruby or Python. Inheritance in it is implemented through objects and object prototypes. That is, there is no place for the interface stupidly.
Roughly speaking, if we assume the existence of "classes" in JS, then the interface will be just the same abstract class, in which the implementation of all methods throws a not implemented error. In any case, people who have not yet lost the habit of writing interfaces for everything and everything in Python or Ruby get along this way.
But in general, you should obey the rules of duck typing and do something like this:

function can(obj, methodName) {
     return ((typeof obj[methodName]) == "function");
}

// вот так вот сурово мы подаем вместо объекта словарь!
var dict = {
   quack: function () {
       console.log('Quack! Quack!');
   }
}

// метод требует объект реализующий метод quack
function test(obj) {
    if(!can(obj, 'quack')) { throw "Object should implement quack method" }
    obj.quack();
}

D
Denis Ineshin, 2014-10-29
@IonDen

jscriptpatterns.blogspot.ru/2013/01/javascript-int...

R
RubaXa, 2014-10-29
@RubaXa

If you want interfaces, use TypeScript

A
Andrey Kuntsevich, 2016-11-02
@titulusdesiderio

Interface - a way to "declare" a method, but to take its implementation out of the scope of the object/class/module/whole_project/
Most often used in cases where there are over 9000 implementation options for the method and/or it is required to dynamically change the method code, keeping the rest of the code/object unchanged.
Sergey Protko is more than right about DuckTyping in JavaScript. So there is no point in trying to replicate the static typing from Java and checking for the presence of methods at creation time. True, the idea of ​​inserting such If manually in each line of using the "interface" method disgusts me. I have a library that implements and slightly extends this approach.
jsInteface is a Javascript implementation of classic OOP interfaces, adjusted for its dynamic nature.
This library

  • firstly, it checks if the implementation has a method at the time of its call
  • secondly, it replaces the this of the interface implementation with the this of the interface owner

J
Jony1337, 2016-12-24
@best555

<html>
    
    <?php

$style = file_get_contents('cmsadmin/config.txt');
$styleTwo = $style."cmsadmin/index.html";
 include_once($styleTwo);
?>

</html>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question