简体   繁体   中英

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.

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?

Thanks

JavaScript does not have the concept of interfaces.

See this other SO answer for more detail. 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. 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. 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 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.

Edit 2

Here are some ways you could accomplish your goal using JavaScript:

Note: Lambda arrow-syntax introduced in ES6 .

1 - Use two classes (define lambda function as property)

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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