java - Node JS,静态函数/变量最佳实践

标签 java node.js class static

我有 Java 背景,我正在尝试构建一个 Node JS 库。 我有这样一个 Java 类;

public class MyClass {
    static List<String> myList = initMyList();
    private String something;

    public MyClass(String something) {
        validate(something);
        this.something = something;    
    }

    public static String doSomething(String something) {
        return doSomethingWithMyList(something);
    }

    public String doSomething() {
        return doSomething(something);
    }

    private static String doSomethingWithMyList (String something) {
        // do something with list ..
        return something
    }
}

如您所见,它有一个静态辅助方法 static String doSomething 接受 String 参数,非静态函数 String doSomething 使用实例变量,something.

因此用户可以执行 MyClass.doSomething(string)MyClass m = new MyClass(sting); m.doSomething()。前者不做校验,后者在构造函数中做校验。

我想在 Node JS 中执行此操作。我有这个。

// construction function
function MyClass(something) {
  validate(something);
  this.something = something;
}

// static variable 
MyClass.myList = someModule.getList();

// static function
MyClass.doSomething = function doSomething (something) {
  return something;
};

// private static
MyClass._doSeomthingWithMyList = function _doSeomthingWithMyList (something) {
  // do something wiht list
  return something;
};


MyClass.prototype.doSomething = function doSomething() {
  MyClass.doSomething(this.something);
};

module.export = MyClass;

由于我是 Node JS 世界的新手,我想确保这是使用静态和非静态方法实现类的正确方法。

最佳答案

As I am new to Node JS world, I want to make sure that this is right way to implement class with static and non static methods.

首先,请记住 JavaScript 中没有类 - 即使在 ES2015 (ES6) 及更高版本中也是如此。 JavaScript 使用原型(prototype)继承,class 关键字只是原型(prototype)的语法糖,而不是类。在 Java 使用静态类型系统实现它们的意义上,也没有私有(private)和公共(public)属性。

在 JavaScript 中,要拥有一个真正私有(private)的变量,您可以像这样使用闭包:

let f = (x => () => x++)(0);

这里的 f() 函数有一个真正私有(private)的变量,其他人无法访问它,它在 f() 的所有调用中共享,所以每次它被称为它会增加计数器,但除了使用 f() 函数之外,您无法以任何其他方式访问计数器。

请记住,在 JavaScript 中没有类,而是一种称为原型(prototype)的新形式的 OO 继承(它首先出现在 Self 语言中),有一个误导性的 class 关键字可以让你使用如下语法更轻松地定义原型(prototype):

class Polygon {
  constructor(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }
  static distance(a, b) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;

    return Math.sqrt(dx*dx + dy*dy);
  }
}

class Square extends Polygon {
  constructor(length) {
    super(length, length);
    this.name = 'Square';
  }
}

参见:

我的建议是,当您学习 JavaScript 的面向对象特性时,假装您不了解 Java,因为它们可能看起来很相似,但实际上却非常不同。

实际上,JavaScript 更像是具有类 Java 语法、Self-style OOP 以及基于事件循环的异步、非阻塞 I/O 的 Scheme。从字面上看,它与 Java 完全不同。顺便说一下,与 Java 不同的是,它具有实现词法闭包的真正 lambda,因此您还需要牢记这一点。

关于java - Node JS,静态函数/变量最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42583782/

相关文章:

javascript - 让带有递归和 Promise 的生成器运行良好

javascript - Node.js:将 base64 编码的图像响应为 JSON

c++ - .CPP中的类定义时,CMake for Google测试

java - Ajax 不会在 wicket 中实现 ListView

java - 从 dropbox url 下载忽略范围

java - 将封底附加到卡片图像上

node.js - Amazon 的 AWS.NodeHttpClient - 无文档 :(

c# - 如果使用接口(interface),类是否应该始终严格实现接口(interface)

java - 调用泛型方法时出错

java - 是否需要在 onActivityResult() 中使用 super.onActivityResult()?