Движок правил wb-rules 1.7: различия между версиями

Строка 197: Строка 197:
Файл сценария scenario.js
Файл сценария scenario.js
<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
var m = require(myModule);
var m = require("myModule");
m.hello("world"); // выведет в лог "Hello from module, world"
m.hello("world"); // выведет в лог "Hello from module, world"
log("The answer is {}", m.answer); // выведет в лог "The answer is 42"
log("The answer is {}", m.answer); // выведет в лог "The answer is 42"
Строка 219: Строка 219:
Файл /etc/wb-rules/modules/myModule.js
Файл /etc/wb-rules/modules/myModule.js
<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
exports.counter = function() {
    if (module.static.count === undefined) {
        module.static.count = 1;
    }
    log("Number of calls: {}", module.static.count);
    module.static.count++;
};
</syntaxhighlight>
Файл сценария scenario1.js
<syntaxhighlight lang="js">
var m = require("myModule");
m.counter();
m.counter();
</syntaxhighlight>
Файл сценария scenario2.js
<syntaxhighlight lang="js">
var m = require("myModule");
m.counter();
m.counter();
m.counter();
</syntaxhighlight>
В результате работы двух скриптов в логе окажется 5 сообщений:


<syntaxhighlight>
Number of calls: 1
Number of calls: 2
Number of calls: 3
Number of calls: 4
Number of calls: 5
</syntaxhighlight>
</syntaxhighlight>
wb_editors
154

правки