简体   繁体   中英

How does the class concept and syntax, especially for encapsulation and member access, of Java compare to the one of Javascript?

I know a lot about Java, but I´m absolutley new to Javascript. I know some background information about the language itself, but thats it. Now im using a certain API and therefor I need Js. So i downloaded Node.js and started to write some code. I´ve realized that there many diffrences to Java and I didnt find any good solutions. Here is an example:

public class Client {

private String username;
private String password;
private String host;
private final int port = 25565;

public Client(String username, String password, String host) {
    this.username = username;
    this.password = password;
    this.host = host;
}

public String getUsername() {
    return username;
}

public int getPort() {
    return port;
}

public String getPassword() {
    return password;
}

public String getHost() {
    return host;
}
}

This is a normal Java class for oop. Could somebody give me an example, how to make something like this in Js and how to access such an class / file (in js) ?

I won't give the same answer as sami, but there is another way to create classes in JS that does allow for variables only accessible to the parent:

function Client(username, passwd, host) {
    const port = 25565; //this is private

    this.getUsername = () => username;  //this is public
    this.getPort = () => port;
    this.getPassword = () => passwd;
    this.getHost = () => host;
}

var client = new Client("john doe", "pass", "www.example.com");
client.getUsername();   //returns "john doe"

In JS, classes are nothing more than functions. Member functions of classes are closures. Also note, how concise the code is due to the arrow functions and how the parameters of the Client constructor function are accessible to the closures within this scope. You can also modify their values as if you declared them with let or var within this scope.

In Nodejs you can create a class, inherits the same as JAVA. But nodejs don't have a variable type or public/private method. For convention add _ with variable name as to represent as a private variable.

To access a js file use module.exports with the class name. Use require for accessing class in another js file.

If you have a setup of package.json with es6 babel. you can directly use export default in place of module.exports and import for accessing class in another js file. More details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

client.js

class Client {
   port = 25565;
   constructor(username, password, host) {
      this._username = username;
      this._password = password;
      this._host = host;
   }

   getUsername() {return this._username}

   getPort() {return this.port}

   getPassword() {return this._password}

   getHost() {return this._host}
}

module.exports = Client;

main.js

var Client = require('./client.js');

console.log(new Client('pr','password', '2020'))

Run: node main.js

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