本文介绍了您如何在Google的v8中包含另一个js文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在v8的.js脚本文件中包含另一个脚本文件?
有< script>标记为HTML,但如何在v8嵌入式程序中完成?
How do you include another script file inside a .js script file in v8?
There's the <script> tag in HTML but how can it be done inside a v8 embedded program?
推荐答案
您必须手动添加此功能,这是我的操作方式:
You have to add this functionality manually, here is how I did it:
Handle<Value> Include(const Arguments& args) {
for (int i = 0; i < args.Length(); i++) {
String::Utf8Value str(args[i]);
// load_file loads the file with this name into a string,
// I imagine you can write a function to do this :)
std::string js_file = load_file(*str);
if(js_file.length() > 0) {
Handle<String> source = String::New(js_file.c_str());
Handle<Script> script = Script::Compile(source);
return script->Run();
}
}
return Undefined();
}
Handle<ObjectTemplate> global = ObjectTemplate::New();
global->Set(String::New("include"), FunctionTemplate::New(Include));
它基本上增加了一个可全局访问的功能,该功能可以在当前上下文中加载和运行javascript文件.我在项目中使用它,就像梦一样.
It basically adds a globally accessible function that can load and run a javascript file within the current context. I use it with my project, works like a dream.
// beginning of main javascript file
include("otherlib.js");
这篇关于您如何在Google的v8中包含另一个js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!