Maison > interface Web > js tutoriel > le corps du texte

Méthode script.createCachedData() dans Node.js

王林
Libérer: 2023-09-04 21:21:08
avant
1178 Les gens l'ont consulté

La méthode

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

script.createCachedData() permet de créer un cache de code qui sera utilisé avec l'option cachedData du constructeur de script. Ce cachedData peut être appelé plusieurs fois sans délai. Cette méthode est l'interface de programmation intégrée au module "Script".

Syntaxe

script.createCachedData()
Copier après la connexion

Paramètres

car il met uniquement en cache les données. Il ne nécessite aucune intervention spécifique de la part de l'utilisateur. Il renvoie uniquement le tampon mis en cache.

Exemple

Créez un fichier appelé createCachedData.js et copiez l'extrait de code suivant. Après avoir créé le fichier, exécutez ce code à l'aide de la commande suivante, comme indiqué dans l'exemple ci-dessous -

node createCachedData.js
Copier après la connexion

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);
Copier après la connexion

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 ... >
Copier après la connexion

Example

Voyons un autre exemple.

// 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);
Copier après la connexion

sortie

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 ... >
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:tutorialspoint.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!