Rumah  >  Artikel  >  hujung hadapan web  >  kaedah script.createCachedData() dalam Node.js

kaedah script.createCachedData() dalam Node.js

王林
王林ke hadapan
2023-09-04 21:21:081148semak imbas

Kaedah

Node.js 中的 script.createCachedData() 方法

script.createCachedData() digunakan untuk mencipta cache kod yang akan digunakan dengan pilihan cachedData bagi pembina skrip. CachedData ini boleh dipanggil beberapa kali tanpa berlengah-lengah. Kaedah ini ialah antara muka pengaturcaraan terbina dalam modul Skrip.

Syntax

script.createCachedData()

Parameter

kerana ia hanya cache data. Ia tidak memerlukan sebarang input khusus daripada pengguna. Ia hanya mengembalikan penimbal cache.

Contoh

Buat fail yang dipanggil createCachedData.js dan salin coretan kod berikut. Selepas mencipta fail, jalankan kod ini menggunakan arahan berikut seperti yang ditunjukkan dalam contoh di bawah -

node createCachedData.js

createCachedData.js

// Node.js program to demonstrate the flow of script.createCachedData() method

// Importing the vm module
const vm = require("vm");

// Defining the script as constant and usng the below function
// to create the cached data for the difference of two numbers.
const script = new vm.Script(`
   function add(a, b) {
      return a - b;
   }
   const x = add(2, 1);
`);

//Creating the cached data without caching the variable
const cacheWithoutx = script.createCachedData();
console.log(cacheWithoutx);

Output

C:\homeode>> node createCachedData.js
<Buffer b5 03 de c0 8a f4 d4 f4 3d 00 00 00 ff 03 00 00 d5 a2 f5 b7 06 00 00
00 00 00 00 00 28 02 00 00 8f 87 4d e3 59 55 98 f9 00 00 00 80 20 00 00 80 00
03 ... >

Contoh

Mari kita lihat satu lagi contoh.

// Node.js program to demonstrate the flow of script.createCachedData() method

// Importing the vm module
const vm = require("vm");

// Defining the script as constant and usng the below function
// to create the cached data for the difference of two numbers.
const script = new vm.Script(`
   function add(a, b) {
      return a - b;
   }
   const x = add(2, 1);
`);

// Calling the runInThisContext from script module
script.runInThisContext();

//Creating the cached data along with caching the variable
const cacheWithx = script.createCachedData();
console.log(cacheWithx);

output

C:\homeode>> node createCachedData.js
<Buffer b5 03 de c0 8a f4 d4 f4 3d 00 00 00 ff 03 00 00 d5 a2 f5 b7 06 00 00
00 00 00 00 00 00 03 00 00 15 80 fd 5d 69 21 3a a9 00 00 00 80 20 00 00 80 38
04 ... >

Atas ialah kandungan terperinci kaedah script.createCachedData() dalam Node.js. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:tutorialspoint.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam