當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js script.createCachedData()用法及代碼示例


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




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | script.createCachedData() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。