Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在没有缓存/持久性的情况下,nodejs需要的最佳方法是什么_Javascript_Node.js - Fatal编程技术网

Javascript 在没有缓存/持久性的情况下,nodejs需要的最佳方法是什么

Javascript 在没有缓存/持久性的情况下,nodejs需要的最佳方法是什么,javascript,node.js,Javascript,Node.js,我正在尝试创建一个require,它对于每个require使用都是唯一的,文件1、2、300等都有一个require-say文件test.js。这可以在一个文件中禁用,但它的变量在其他文件中不会被触及 File1.js const test = require("./test.js"); // NOTE enabled boolean in test is default = true test.enabled = false; // or test.disable(); test.sayHel

我正在尝试创建一个require,它对于每个require使用都是唯一的,文件1、2、300等都有一个require-say文件test.js。这可以在一个文件中禁用,但它的变量在其他文件中不会被触及

File1.js

const test = require("./test.js"); // NOTE enabled boolean in test is default = true test.enabled = false; // or test.disable(); test.sayHello(); // will output nothing as enabled = false 常量测试=要求(“./test.js”); //注意:测试中启用的布尔值默认为true test.enabled=false;//或test.disable(); test.sayHello();//将不输出任何内容,因为已启用=false File2.js

const test = require("./test.js"); test.sayHello(); // Should output hello but it as file1 set enabled to false it dosnt 常量测试=要求(“./test.js”); test.sayHello();//应输出hello,但将其设置为file1并将其设置为false dosnt 要实现此功能,test.js会是什么样子

目前,我必须通过module.exports函数中的一个参数来执行此操作,这并不理想。eg disable是测试函数的直接返回,然后是enable/disable的第二个可选参数。那是我

谢谢

<> d

,即使您可以<代码>要求 Cache,我也会考虑,对于您的特定情况,一个坏的实践。

相反,require调用应该返回一个
,然后在每个文件上使用该类的新实例,并在需要时禁用/启用该实例

test.js

class Test {

    disable() {
        this.disable = true;
    }

    sayHello() {
        if(this.disable)
            return false;

        console.log('hello')
    }

}

module.exports = Test;
const Test = require('./test.js');
const test = new Test();
test.disable();
test.sayHello(); // nothing is printed

const otherTest = new Test();
otherTest.sayHello(); // 'hello'
index.js

class Test {

    disable() {
        this.disable = true;
    }

    sayHello() {
        if(this.disable)
            return false;

        console.log('hello')
    }

}

module.exports = Test;
const Test = require('./test.js');
const test = new Test();
test.disable();
test.sayHello(); // nothing is printed

const otherTest = new Test();
otherTest.sayHello(); // 'hello'

忘记了返回类/函数本身,而不是它的实例化。。。嗯。。。这些小东西呃。。。谢谢你的回答:)的确,那些小东西:),很高兴能帮上忙!