Node.js util.types.isExternal()方法
util.types.isExternal()方法是util模块内置的应用程序编程接口,用于检查值是否是node.js中的本机外部值。
语法:
util.types.isExternal( value )
参数: 该方法接受一个参数,如上所述并以下面的方式进行描述。
- value: 这是任意数据类型的必需参数。
返回值: 如果该值是原生外部值,则返回真,否则返回假。
以下示例演示了在Node.js中使用util.types.isExternal()方法:
示例1:
// 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
const 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')));
输出:
false
true
false
false
示例2:
// 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
const v1 = new Date();
const { JSStream } = process.binding('js_stream');
const ext = (new JSStream())._externalStream;
const 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