script.createCachedData()方法是脚本模块的内置应用程序编程接口,用于创建代码缓存,该缓存可以与脚本构造函数的cachedData选项一起使用。可以随时随地调用它。
用法:
script.createCachedData()
参数:此方法不接受任何参数。
返回值:它返回缓冲区。
以下示例说明了Node.js中script.createCachedData()方法的使用:
范例1:
// Node.js program to demonstrate the
// script.createCachedData() method
// Including vm module
const vm = require("vm");
// Constructing script and defining a
// function add inside it
const script = new vm.Script(`
function add(a, b) {
return a + b;
}
const x = add(1, 2);
`);
// Calling createCachedData without caching
// the varibale x used above
const cacheWithoutx = script.createCachedData();
console.log(cacheWithoutx);
输出:
<Buffer b5 03 de c0 8a f4 d4 f4 3f 00 00 00 ff 03 00 00 d5 a2 f5 b7 06 00 00 00 00 00 00 00 28 02 00 00 af 79 f4 0d a0 8c bf b8 00 00 00 80 20 0000 80 00 03 ... >
范例2:
// Node.js program to demonstrate the
// script.createCachedData() method
// Including vm module
const vm = require("vm");
// Constructing script and defining a
// function add inside it
const script = new vm.Script(`
function add(a, b) {
return a + b;
}
const x = add(1, 2);
`);
// Calling runInThisContext method
script.runInThisContext();
// Calling createCachedData with caching
// the varibale x used above
const cacheWithx = script.createCachedData();
console.log(cacheWithx);
输出:
<Buffer b5 03 de c0 8a f4 d4 f4 3f 00 00 00 ff 03 00 00 d5 a2 f5 b7 06 00 00 00 00 00 00 00 00 03 00 00 03 67 df 75 6d 4c 36 07 00 00 00 80 20 0000 80 38 04 ... >
参考: https://nodejs.org/api/vm.html#vm_script_createcacheddata
相关用法
- Node.js GM threshold()用法及代码示例
- Node.js GM randomThreshold()用法及代码示例
- Node.js GM implode()用法及代码示例
- Node.js GM edge()用法及代码示例
- Node.js GM recolor()用法及代码示例
- Node.js GM drawRectangle()用法及代码示例
- Node.js GM quality()用法及代码示例
- Node.js GM raise()用法及代码示例
- Node.js GM drawPolygon()用法及代码示例
- Node.js GM thumbnail()用法及代码示例
注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | script.createCachedData() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。