簡體   English   中英

“instanceof Date”在 BigQuery UDF 中的行為不符合預期

[英]`instanceof Date` not behaving as expected within a BigQuery UDF

任何人都知道為什么以下結果是false而不是true

CREATE TEMPORARY FUNCTION test()
RETURNS BOOL
LANGUAGE js
AS
"""
const d = new Date();
return d instanceof Date;
""";
SELECT test();

返回false (意外)


解決方法:

CREATE TEMPORARY FUNCTION test()
RETURNS BOOL
LANGUAGE js
AS
"""
const d = new Date();
return Object.prototype.toString.call(d) === '[object Date]';
""";
SELECT test();

返回true (如預期)

instanceof Date接縫不起作用。

對於其他對象,如String ,它工作正常。

有一個 JavaScript 解決方法

CREATE TEMPORARY FUNCTION test()
RETURNS  BOOL
LANGUAGE js
AS
"""
var  d = new Date();
var s= new String('String created with constructor');
//return s instanceof String;
return typeof d.getMonth === 'function';
""";

SELECT test();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM