📜  Node.js util.types.isExternal() 方法

📅  最后修改于: 2022-05-13 01:56:34.877000             🧑  作者: Mango

Node.js util.types.isExternal() 方法

util.types.isExternal() 方法是 util 模块的内置应用程序编程接口,用于检查该值是否是 node.js 中的本机外部值。

句法:

util.types.isExternal( value )

参数:此方法接受上面提到的和下面描述的单个参数。

  • value:它是任何数据类型的必需参数。

返回值:这将返回一个布尔值,如果该值是本地外部值,则返回 TRUE,否则返回 FALSE。

下面的示例说明了 Node.js 中 util.types.isExternal() 方法的使用:

示例 1:

Javascript
// Node.js program to demonstrate the
// util.types.isExternal() Method
  
// Allocating util module
const util = require('util');
  
// Value to be passed as parameter
// of util.types.isExternal() method
var v1 = new Date();
const { JSStream } = process.binding('js_stream');
const external = (new JSStream())._externalStream;
  
// Printing the returned value from
// util.types.isExternal() method
  
console.log(util.types.isExternal(v1));
console.log(util.types.isExternal(external));
console.log(util.types.isExternal(0));
console.log(util.types.isExternal(new String('foo')));


Javascript
// Node.js program to demonstrate the
// util.types.isExternal() Method
  
// Allocating util module
const util = require('util');
  
// Value to be passed as parameter
// of util.types.isExternal() method
var v1 = new Date();
const { JSStream } = process.binding('js_stream');
const ext = (new JSStream())._externalStream;
var str = new String('Geeks')
  
// Calling
// util.types.isExternal() method
if (util.types.isExternal(v1))
    console.log("It is a native External value.");
else
    console.log("It is not a native External value.");
  
if (util.types.isExternal(ext))
    console.log("It is a native External value.");
else
    console.log("It is not a native External value.");
  
if (util.types.isExternal(0))
    console.log("It is a native External value.");
else
    console.log("It is not a native External value.");
  
if (util.types.isExternal(str))
    console.log("It is a native External value.");
else
    console.log("It is not a native External value.");


输出:

false
true
false
false

示例 2:

Javascript

// Node.js program to demonstrate the
// util.types.isExternal() Method
  
// Allocating util module
const util = require('util');
  
// Value to be passed as parameter
// of util.types.isExternal() method
var v1 = new Date();
const { JSStream } = process.binding('js_stream');
const ext = (new JSStream())._externalStream;
var str = new String('Geeks')
  
// Calling
// util.types.isExternal() method
if (util.types.isExternal(v1))
    console.log("It is a native External value.");
else
    console.log("It is not a native External value.");
  
if (util.types.isExternal(ext))
    console.log("It is a native External value.");
else
    console.log("It is not a native External value.");
  
if (util.types.isExternal(0))
    console.log("It is a native External value.");
else
    console.log("It is not a native External value.");
  
if (util.types.isExternal(str))
    console.log("It is a native External value.");
else
    console.log("It is not a native External value.");

输出:

It is not a native External value.
It is a native External value.
It is not a native External value.
It is not a native External value.

参考: https://nodejs.org/api/util.html#util_util_types_isexternal_value