如何在javascript/nodejs(ES6)中创建静态函数/对象 [英] How create static functions/objects in javascript/nodejs (ES6)

查看:358
本文介绍了如何在javascript/nodejs(ES6)中创建静态函数/对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Javascript/Node JS创建一个静态类.我用过google,但找不到任何有用的示例.

我想用Javascript ES6创建类似这样的东西(C#):

public static MyStaticClass {
   public static void someMethod() {
      //do stuff here
   }
}

目前,我有这个类,但是我认为这段代码每次在"require"中被调用时都会创建一个新实例.

function MyStaticClass() {
   let someMethod = () => {
      //do some stuff
   }
}
var myInstance = new MyStaticClass();
module.exports = factory;

解决方案

请注意,JS是 prototype- ,而不是基于类.

您不必在类中多次创建类来访问其方法,而只需在对象中创建一个方法,例如

var MyStaticClass = {
    someMethod: function () {
        console.log('Doing someMethod');
    }
}

MyStaticClass.someMethod(); // Doing someMethod

从JS开始,一切都是对象(原始类型除外) + undefined + null).就像在上面创建someMethod函数时一样,您实际上创建了一个新的函数对象,可以在MyStaticClass对象中使用someMethod进行访问. (这就是为什么您可以访问someMethod对象的属性,例如MyStaticClass.someMethod.prototypeMyStaticClass.someMethod.name)

但是,如果您发现使用类更方便. ES6现在可以与静态方法一起使用. >

例如

MyStaticClass.js

class MyStaticClass {
    static someMethod () {
        console.log('Doing someMethod');
    }

    static anotherMethod () {
        console.log('Doing anotherMethod');
    }
}

module.exports = MyStaticClass;

Main.js

var MyStaticClass = require("./MyStaticClass");

MyStaticClass.someMethod(); // Doing someMethod
MyStaticClass.anotherMethod(); // Doing anotherMethod

I want to create a static class using Javascript/Node JS. I used google but i can't find any usefull example.

I want to create in Javascript ES6 something like this (C#):

public static MyStaticClass {
   public static void someMethod() {
      //do stuff here
   }
}

For now, I have this class, but I think that this code will creates a new instance every time that it be called from "require".

function MyStaticClass() {
   let someMethod = () => {
      //do some stuff
   }
}
var myInstance = new MyStaticClass();
module.exports = factory;

解决方案

Note that JS is prototype-based programming, instead of class-based.

Instead of creating the class multiple times to access its method, you can just create a method in an object, like

var MyStaticClass = {
    someMethod: function () {
        console.log('Doing someMethod');
    }
}

MyStaticClass.someMethod(); // Doing someMethod

Since in JS, everything is an object (except primitive types + undefined + null). Like when you create someMethod function above, you actually created a new function object that can be accessed with someMethod inside MyStaticClass object. (That's why you can access the properties of someMethod object like MyStaticClass.someMethod.prototype or MyStaticClass.someMethod.name)

However, if you find it more convenient to use class. ES6 now works with static methods.

E.g.

MyStaticClass.js

class MyStaticClass {
    static someMethod () {
        console.log('Doing someMethod');
    }

    static anotherMethod () {
        console.log('Doing anotherMethod');
    }
}

module.exports = MyStaticClass;

Main.js

var MyStaticClass = require("./MyStaticClass");

MyStaticClass.someMethod(); // Doing someMethod
MyStaticClass.anotherMethod(); // Doing anotherMethod

这篇关于如何在javascript/nodejs(ES6)中创建静态函数/对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆