简体   繁体   English

函数式编程接口js

[英]Functional programming interfaces js

I see on internet there is interfaces for functional programming.我在互联网上看到有函数式编程的接口。

There are some samples for other languages but my question is can i make interfaces with javascript.有一些其他语言的示例,但我的问题是我可以使用 javascript 制作接口。

I dont need everything to be very strictly.我不需要一切都非常严格。

For example interface for getting rows from the database:例如从数据库中获取行的接口:

In OOP:在面向对象中:

interface DBGet {
 public function getResults($filters){}
}

and implementation:和实施:

class MySQLDb implements DBGet {
  public function getResults($filters){
    ......
  }
}

Can I transform the code in JavaScript with functional programming?我可以使用函数式编程转换 JavaScript 中的代码吗?

Thanks谢谢

JavaScript does not have the concept of interfaces. JavaScript 没有接口的概念。

See this other SO answer for more detail.有关更多详细信息,请参阅此其他 SO 答案 It essentially says the following:它本质上是这样说的:

There's no notion of "this class must have these functions" (that is, no interfaces per se), because:没有“这个类必须具有这些功能”的概念(即,本身没有接口),因为:

  1. JavaScript inheritance is based on objects, not classes. JavaScript 继承基于对象,而不是类。 That's not a big deal until you realize:这没什么大不了的,直到你意识到:
  2. JavaScript is an extremely dynamically typed language -- you can create an object with the proper methods, which would make it conform to the interface, and then undefine all the stuff that made it conform. JavaScript 是一种非常动态的类型语言——您可以使用适当的方法创建一个对象,这将使其符合接口,然后取消定义所有使其符合的内容。 It'd be so easy to subvert the type system -- even accidentally!颠覆类型系统是如此容易——即使是偶然的! -- that it wouldn't be worth it to try and make a type system in the first place. -- 首先尝试创建类型系统是不值得的。

Edit编辑

You could use TypeScript, however.但是,您可以使用 TypeScript。TypeScript introduces interfaces for a more traditional OO type system implementation, however, there are some caveats to using them and they simply get stripped out of the code when it is converted to vanilla JavaScript anyway.TypeScript为更传统的 OO 类型系统实现引入了接口,但是,使用它们有一些注意事项,无论如何,当它转换为 vanilla JavaScript 时,它们只会从代码中剥离出来。

Edit 2编辑 2

Here are some ways you could accomplish your goal using JavaScript:以下是您可以使用 JavaScript 实现目标的一些方法:

Note: Lambda arrow-syntax introduced in ES6 .注意:在ES6 中引入的 Lambda 箭头语法。

1 - Use two classes (define lambda function as property) 1 - 使用两个类(将 lambda 函数定义为属性)

class MySqlDbGet {
    getResults = (filters) => {
        // MySQL implementation of "getResults"
    };
}

class PostgreSqlDbGet {
    getResults = (filters) => {
        // PostgreSQL implementation of "getResults"
    };
}

methodThatTakesAFunction(new MySqlDbGet().getResults);
methodThatTakesAFunction(new PostgreSqlDbGet().getResults);

2 - Use two objects (define lambda function as property) 2 - 使用两个对象(将 lambda 函数定义为属性)

const mySqlDbGet = {
    getResults: () => {
        // MySQL implementation of "getResults"
    }
};

const postgreSqlDbGet = {
    getResults: () => {
        // PostgreSQL implementation of "getResults"
    }
};

methodThatTakesAFunction(mySqlDbGet.getResults);
methodThatTakesAFunction(postgreSqlDbGet.getResults);

3 - Use two variables to hold your lambda functions 3 - 使用两个变量来保存您的 lambda 函数

const mySqlGetResults = () => {
    // MySQL implementation of "getResults"
};

const postgreSqlGetResults = () => {
    // PostgreSQL implementation of "getResults"
};

methodThatTakesAFunction(mySqlGetResults);
methodThatTakesAFunction(postgreSqlGetResults);

The main point still stands, however: JavaScript does not hold your objects to a certain contract.然而,要点仍然存在:JavaScript 不会让您的对象遵守某种契约。 Therefore, at any time during your program, you could re-assign mySqlGetResults as "som string" and pass it anywhere you like without getting any error messages until runtime.因此,在程序运行期间的任何时候,您都可以将mySqlGetResults重新分配为"som string"并将其传递到您喜欢的任何位置,而不会在运行之前收到任何错误消息。 It is up to you to do checks on variables that may not be what you think they are, and to handle any situations where you may not be sure if an object is defined as you think.由您来检查可能与您认为的不同的变量,并处理您可能不确定对象是否按您的想法定义的任何情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM