Java的类概念和语法,特别是封装和成员访问方面,与JavaScript相比如何?

huangapple go评论81阅读模式
英文:

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

问题

我对Java有很多了解,但对于JavaScript完全是新手。我了解一些关于语言本身的背景信息,但仅限于此。现在我正在使用特定的API,因此我需要用到Js。所以我下载了Node.js并开始编写一些代码。我意识到与Java有许多不同之处,我找不到任何好的解决方案。这里是一个示例:

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

    getUsername() {
        return this.username;
    }

    getPort() {
        return this.port;
    }

    getPassword() {
        return this.password;
    }

    getHost() {
        return this.host;
    }
}

这是一个在面向对象编程中的常见Java类。有人能给我一个示例,如何在Js中创建类似的内容,以及如何访问这样一个类/文件(在js中)?

英文:

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) ?

答案1

得分: 1

我不会像sami一样给出相同的答案,但在JS中创建类的另一种方式可以使变量仅对父级可访问:

function Client(username, passwd, host) {
    const port = 25565; //这是私有的

    this.getUsername = () => username;  //这是公开的
    this.getPort = () => port;
    this.getPassword = () => passwd;
    this.getHost = () => host;
}

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

在JS中,类实际上只是函数。类的成员函数是闭包。还要注意,由于箭头函数,代码非常简洁,并且Client构造函数中的参数在此范围内的闭包中可访问。您还可以像在此范围内使用letvar声明它们一样修改它们的值。

英文:

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.

答案2

得分: -1

在Node.js中,您可以创建一个类,与JAVA中的方式类似。但是Node.js没有变量类型或公共/私有方法的概念。按照约定,可以在变量名前面加下划线 _ 来表示为私有变量。

要访问一个JavaScript文件,使用 module.exports 并附上类名。
使用 require 来在另一个JavaScript文件中访问类。

如果您在package.json中设置了ES6 Babel,您可以直接使用 export default 代替 module.exports
并且可以使用 import 来在另一个JavaScript文件中访问类。更多详情: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

英文:

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

huangapple
  • 本文由 发表于 2020年9月29日 17:55:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64117198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定