Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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
freeswitch脚本中的mod_v8如何满足javascript文件的需求?_Javascript_Module_Lua_Require_Freeswitch - Fatal编程技术网

freeswitch脚本中的mod_v8如何满足javascript文件的需求?

freeswitch脚本中的mod_v8如何满足javascript文件的需求?,javascript,module,lua,require,freeswitch,Javascript,Module,Lua,Require,Freeswitch,我真的被困在这里了。虽然mod_lua中模块的require与lua解释器的工作方式类似,但mod_v8中的require()似乎“包含”了整个脚本。我还没有找到一种方法使works只导入脚本中需要的模块(不是节点模块) 例如,在脚本中,我有一些如下所示: //core/dtmf_1.js const a = (arg) => { return arg * 2 } const b = (arg) => { return arg / 2 } //I get an error DO

我真的被困在这里了。虽然mod_lua中模块的require与lua解释器的工作方式类似,但mod_v8中的require()似乎“包含”了整个脚本。我还没有找到一种方法使works只导入脚本中需要的模块(不是节点模块)

例如,在脚本中,我有一些如下所示:

//core/dtmf_1.js

const a = (arg) => { return arg * 2 }
const b = (arg) => { return arg / 2 }


//I get an error DONT WORKS
exports.a = a
exports.b = b
下面的示例对我来说也不起作用,但不会抛出错误

//core/dtmf_2.js

export function a = (arg) => { return arg * 2 }
export function b = (arg) => { return arg / 2 }
否则,当我打电话时

//ivr.js
import a from 'core/dtmf_2.js' 
“导入”中出现错误

但如果我这么做:

//core/dtmf_3.js
const function a = (arg) => { return arg * 2 }
const function b = (arg) => { return arg / 2 }
//ivr.js
要求('core/dtmf_3.js')

log(b(30))FreeSwitch使用V8,而不是Node.js,这样您就不会得到
require
export
函数。对于包含JavaScript脚本,您可以使用FreeSwitch
include
require
关键字,它们是相同的
Include
将包含整个脚本,因此您无法执行下面的示例,因为您正在加载math.js两次,因此将发生异常:
SyntaxError:Identifier'pi'已声明(接近:“const pi=3.14;”

math.js

const pi = 3.14;

function circleArea(radius){
    return radius * radius * pi;
}

function sum(a, b){
    return a + b;
}
(a, b) => {
    return a + b;
}
(() => {

    const pi = 3.14;

    function circleArea(radius){
        return radius * radius * pi;
    }

    function sum(a, b){
        return a + b;
    }

    return { circleArea, sum };

})();
(() => {
    log("Math loaded...");
})();
(() => {
    console.log("Math loaded...");
})();
(() => {

    log("Math loaded...");

    var pi = 3.14;

    return { pi };

})();
circle.js

include('/etc/freeswitch/test/math.js');

function getArea(radius){
    return circleArea(radius);
}
main.js

include('/etc/freeswitch/test/circle.js');
include('/etc/freeswitch/test/math.js');

//math.js
log( sum(5,10) );

//circle.js
log( getArea(10) );
const sum = include('/etc/freeswitch/test/math.js');

//logs 15
log( sum(5, 10) );
const math = include('/etc/freeswitch/test/math.js');
const { sum, circleArea } = include('/etc/freeswitch/test/math.js');

//with module
log( math.circleArea(5) ); // logs 78.5
log( math.sum(5, 10) );    // logs 15

//direct call
log( circleArea(5) );      // logs 78.5
log( sum(5, 10) );         // logs 15
const math1 = include('/etc/freeswitch/test/math.js');
const math2 = include('/etc/freeswitch/test/math.js');
const math1 = require('./math.js');
const math2 = require('./math.js');
include('/etc/freeswitch/include.js');

const math1 = _include('/etc/freeswitch/test/math.js');
const math2 = _include('/etc/freeswitch/test/math.js');

math1.pi = 10;
math2.pi = 20;

log(math1.pi);
log(math2.pi);
将函数包含到变量中
include
实现允许您将代码从脚本“复制粘贴”到首先调用
include
的脚本中,以便您可以利用它将代码加载到变量中。例如:

math.js

const pi = 3.14;

function circleArea(radius){
    return radius * radius * pi;
}

function sum(a, b){
    return a + b;
}
(a, b) => {
    return a + b;
}
(() => {

    const pi = 3.14;

    function circleArea(radius){
        return radius * radius * pi;
    }

    function sum(a, b){
        return a + b;
    }

    return { circleArea, sum };

})();
(() => {
    log("Math loaded...");
})();
(() => {
    console.log("Math loaded...");
})();
(() => {

    log("Math loaded...");

    var pi = 3.14;

    return { pi };

})();
main.js

include('/etc/freeswitch/test/circle.js');
include('/etc/freeswitch/test/math.js');

//math.js
log( sum(5,10) );

//circle.js
log( getArea(10) );
const sum = include('/etc/freeswitch/test/math.js');

//logs 15
log( sum(5, 10) );
const math = include('/etc/freeswitch/test/math.js');
const { sum, circleArea } = include('/etc/freeswitch/test/math.js');

//with module
log( math.circleArea(5) ); // logs 78.5
log( math.sum(5, 10) );    // logs 15

//direct call
log( circleArea(5) );      // logs 78.5
log( sum(5, 10) );         // logs 15
const math1 = include('/etc/freeswitch/test/math.js');
const math2 = include('/etc/freeswitch/test/math.js');
const math1 = require('./math.js');
const math2 = require('./math.js');
include('/etc/freeswitch/include.js');

const math1 = _include('/etc/freeswitch/test/math.js');
const math2 = _include('/etc/freeswitch/test/math.js');

math1.pi = 10;
math2.pi = 20;

log(math1.pi);
log(math2.pi);
将自调用函数包含到变量中 现在,将同一个脚本加载到全局范围不会有问题。我们可以通过使用自调用函数将其提升到另一个级别

math.js

const pi = 3.14;

function circleArea(radius){
    return radius * radius * pi;
}

function sum(a, b){
    return a + b;
}
(a, b) => {
    return a + b;
}
(() => {

    const pi = 3.14;

    function circleArea(radius){
        return radius * radius * pi;
    }

    function sum(a, b){
        return a + b;
    }

    return { circleArea, sum };

})();
(() => {
    log("Math loaded...");
})();
(() => {
    console.log("Math loaded...");
})();
(() => {

    log("Math loaded...");

    var pi = 3.14;

    return { pi };

})();
main.js

include('/etc/freeswitch/test/circle.js');
include('/etc/freeswitch/test/math.js');

//math.js
log( sum(5,10) );

//circle.js
log( getArea(10) );
const sum = include('/etc/freeswitch/test/math.js');

//logs 15
log( sum(5, 10) );
const math = include('/etc/freeswitch/test/math.js');
const { sum, circleArea } = include('/etc/freeswitch/test/math.js');

//with module
log( math.circleArea(5) ); // logs 78.5
log( math.sum(5, 10) );    // logs 15

//direct call
log( circleArea(5) );      // logs 78.5
log( sum(5, 10) );         // logs 15
const math1 = include('/etc/freeswitch/test/math.js');
const math2 = include('/etc/freeswitch/test/math.js');
const math1 = require('./math.js');
const math2 = require('./math.js');
include('/etc/freeswitch/include.js');

const math1 = _include('/etc/freeswitch/test/math.js');
const math2 = _include('/etc/freeswitch/test/math.js');

math1.pi = 10;
math2.pi = 20;

log(math1.pi);
log(math2.pi);
但是,每次您想要加载math.js时,它都会充当自己的模块,并且会多次加载,而不像Node.js模块那样。例如:

自由开关

math.js

const pi = 3.14;

function circleArea(radius){
    return radius * radius * pi;
}

function sum(a, b){
    return a + b;
}
(a, b) => {
    return a + b;
}
(() => {

    const pi = 3.14;

    function circleArea(radius){
        return radius * radius * pi;
    }

    function sum(a, b){
        return a + b;
    }

    return { circleArea, sum };

})();
(() => {
    log("Math loaded...");
})();
(() => {
    console.log("Math loaded...");
})();
(() => {

    log("Math loaded...");

    var pi = 3.14;

    return { pi };

})();
main.js

include('/etc/freeswitch/test/circle.js');
include('/etc/freeswitch/test/math.js');

//math.js
log( sum(5,10) );

//circle.js
log( getArea(10) );
const sum = include('/etc/freeswitch/test/math.js');

//logs 15
log( sum(5, 10) );
const math = include('/etc/freeswitch/test/math.js');
const { sum, circleArea } = include('/etc/freeswitch/test/math.js');

//with module
log( math.circleArea(5) ); // logs 78.5
log( math.sum(5, 10) );    // logs 15

//direct call
log( circleArea(5) );      // logs 78.5
log( sum(5, 10) );         // logs 15
const math1 = include('/etc/freeswitch/test/math.js');
const math2 = include('/etc/freeswitch/test/math.js');
const math1 = require('./math.js');
const math2 = require('./math.js');
include('/etc/freeswitch/include.js');

const math1 = _include('/etc/freeswitch/test/math.js');
const math2 = _include('/etc/freeswitch/test/math.js');

math1.pi = 10;
math2.pi = 20;

log(math1.pi);
log(math2.pi);
这会打印“数学加载…”两次

Node.js

math.js

const pi = 3.14;

function circleArea(radius){
    return radius * radius * pi;
}

function sum(a, b){
    return a + b;
}
(a, b) => {
    return a + b;
}
(() => {

    const pi = 3.14;

    function circleArea(radius){
        return radius * radius * pi;
    }

    function sum(a, b){
        return a + b;
    }

    return { circleArea, sum };

})();
(() => {
    log("Math loaded...");
})();
(() => {
    console.log("Math loaded...");
})();
(() => {

    log("Math loaded...");

    var pi = 3.14;

    return { pi };

})();
main.js

include('/etc/freeswitch/test/circle.js');
include('/etc/freeswitch/test/math.js');

//math.js
log( sum(5,10) );

//circle.js
log( getArea(10) );
const sum = include('/etc/freeswitch/test/math.js');

//logs 15
log( sum(5, 10) );
const math = include('/etc/freeswitch/test/math.js');
const { sum, circleArea } = include('/etc/freeswitch/test/math.js');

//with module
log( math.circleArea(5) ); // logs 78.5
log( math.sum(5, 10) );    // logs 15

//direct call
log( circleArea(5) );      // logs 78.5
log( sum(5, 10) );         // logs 15
const math1 = include('/etc/freeswitch/test/math.js');
const math2 = include('/etc/freeswitch/test/math.js');
const math1 = require('./math.js');
const math2 = require('./math.js');
include('/etc/freeswitch/include.js');

const math1 = _include('/etc/freeswitch/test/math.js');
const math2 = _include('/etc/freeswitch/test/math.js');

math1.pi = 10;
math2.pi = 20;

log(math1.pi);
log(math2.pi);
这会打印“数学加载…”一次

仅将自调用函数包含到变量中一次 从这里,您可以编写自己的include脚本(这仍然是一种糟糕的方法,但应该可以工作)。大概是这样的:

include.js

if (typeof _include === 'undefined') {

    var _loadedScripts = {};

    function _include(script){
        if (!_loadedScripts.hasOwnProperty(script)){
            _loadedScripts[script] = include(script);
        }
        return _loadedScripts[script];
    }

}
if (typeof _include === 'undefined') {

    var _loadedScripts = {};

    function _include(script){
        if (!_loadedScripts.hasOwnProperty(script)){
            _loadedScripts[script] = require(script);
        }
        return _loadedScripts[script];
    }

    include = () => {  };

}
然后您可以像以前一样编写模块:

math.js

const pi = 3.14;

function circleArea(radius){
    return radius * radius * pi;
}

function sum(a, b){
    return a + b;
}
(a, b) => {
    return a + b;
}
(() => {

    const pi = 3.14;

    function circleArea(radius){
        return radius * radius * pi;
    }

    function sum(a, b){
        return a + b;
    }

    return { circleArea, sum };

})();
(() => {
    log("Math loaded...");
})();
(() => {
    console.log("Math loaded...");
})();
(() => {

    log("Math loaded...");

    var pi = 3.14;

    return { pi };

})();
当您想要包含脚本时:

main.js

include('/etc/freeswitch/test/circle.js');
include('/etc/freeswitch/test/math.js');

//math.js
log( sum(5,10) );

//circle.js
log( getArea(10) );
const sum = include('/etc/freeswitch/test/math.js');

//logs 15
log( sum(5, 10) );
const math = include('/etc/freeswitch/test/math.js');
const { sum, circleArea } = include('/etc/freeswitch/test/math.js');

//with module
log( math.circleArea(5) ); // logs 78.5
log( math.sum(5, 10) );    // logs 15

//direct call
log( circleArea(5) );      // logs 78.5
log( sum(5, 10) );         // logs 15
const math1 = include('/etc/freeswitch/test/math.js');
const math2 = include('/etc/freeswitch/test/math.js');
const math1 = require('./math.js');
const math2 = require('./math.js');
include('/etc/freeswitch/include.js');

const math1 = _include('/etc/freeswitch/test/math.js');
const math2 = _include('/etc/freeswitch/test/math.js');

math1.pi = 10;
math2.pi = 20;

log(math1.pi);
log(math2.pi);
这将产生:

Math loaded...
20
20
[编辑]

Include.js是唯一一个未作为“模块”加载的脚本,因此它将被多次加载。您可以通过将默认包含函数替换为您自己的,而不是使用所需的包含函数来防止这种情况

include.js

if (typeof _include === 'undefined') {

    var _loadedScripts = {};

    function _include(script){
        if (!_loadedScripts.hasOwnProperty(script)){
            _loadedScripts[script] = include(script);
        }
        return _loadedScripts[script];
    }

}
if (typeof _include === 'undefined') {

    var _loadedScripts = {};

    function _include(script){
        if (!_loadedScripts.hasOwnProperty(script)){
            _loadedScripts[script] = require(script);
        }
        return _loadedScripts[script];
    }

    include = () => {  };

}

现在,如果Include.js已经包含在内,那么每次调用Include(“Include.js”)时,它都不会再次加载。

是的,它似乎只包含整个脚本,而不像节点那样包含名称空间……您的答案非常完美。非常感谢。