[英]How can I check whether a variable is defined in Node.js?
我正在研究 node.js 中的一個程序,它實際上是 js。
我有一個變量:
var query = azure.TableQuery...
看起來這行代碼有時沒有執行。
我的問題是:
我怎樣才能做到這樣的條件:
if this variable is defined do this.
else do this.
我不能在 js 中做(query!= null)
我想看看這個變量是否被定義做一些事情。 這個怎么做
if ( typeof query !== 'undefined' && query )
{
//do stuff if query is defined and not null
}
else
{
}
確定屬性是否存在(但不是虛假值):
if (typeof query !== 'undefined' && query !== null){
doStuff();
}
通常使用
if (query){
doStuff();
}
足夠了。 請注意:
if (!query){
doStuff();
}
即使查詢是帶有falsy值的現有變量(0,false,undefined或null),doStuff()也會執行
順便說一下,有一種性感的咖啡方式:
if object?.property? then doStuff()
編譯為:
if ((typeof object !== "undefined" && object !== null ? object.property : void 0) != null)
{
doStuff();
}
對我來說,像一個表達
if (typeof query !== 'undefined' && query !== null){
// do stuff
}
我想要使用它的頻率比我想要的要復雜得多。 也就是說,測試變量是否定義為/ null是我經常做的事情。 我希望這樣的測試很簡單。 為了解決這個問題,我首先嘗試將上面的代碼定義為函數,但是節點只給我一個語法錯誤,告訴我函數調用的參數是未定義的。 沒有用! 因此,搜索並研究這一點,我找到了一個解決方案。 不是每個人都有。 我的解決方案涉及使用Sweet.js來定義宏。 我是這樣做的:
這是宏(文件名:macro.sjs):
// I had to install sweet using:
// npm install --save-dev
// See: https://www.npmjs.com/package/sweetbuild
// Followed instructions from https://github.com/mozilla/sweet.js/wiki/node-loader
// Initially I just had "($x)" in the macro below. But this failed to match with
// expressions such as "self.x. Adding the :expr qualifier cures things. See
// http://jlongster.com/Writing-Your-First-Sweet.js-Macro
macro isDefined {
rule {
($x:expr)
} => {
(( typeof ($x) === 'undefined' || ($x) === null) ? false : true)
}
}
// Seems the macros have to be exported
// https://github.com/mozilla/sweet.js/wiki/modules
export isDefined;
以下是宏的使用示例(在example.sjs中):
function Foobar() {
var self = this;
self.x = 10;
console.log(isDefined(y)); // false
console.log(isDefined(self.x)); // true
}
module.exports = Foobar;
這是主節點文件:
var sweet = require('sweet.js');
// load all exported macros in `macros.sjs`
sweet.loadMacro('./macro.sjs');
// example.sjs uses macros that have been defined and exported in `macros.sjs`
var Foobar = require('./example.sjs');
var x = new Foobar();
除此之外,除了必須安裝Sweet之外,設置宏,並在代碼中加載Sweet,它可能會使Node中的錯誤報告復雜化。 它增加了第二層解析。 還沒有用過這么多,所以應該看看它是如何成為第一手的。 我喜歡Sweet雖然我想念宏,所以會盡力堅持下去!
如果您的變量未聲明也未定義:
if ( typeof query !== 'undefined' ) { ... }
如果您的變量已聲明但未定義。 (假設這里的情況是變量可能沒有定義,但它可以是任何其他虛假值,如false
或""
)
if ( query ) { ... }
如果您的變量已聲明但可以是undefined
或null
:
if ( query != null ) { ... } // undefined == null
對於簡單的任務,我經常這樣做:
var undef;
// Fails on undefined variables
if (query !== undef) {
// variable is defined
} else {
// else do this
}
或者,如果您只是想檢查一個空值...
var undef;
// Fails on undefined variables
// And even fails on null values
if (query != undef) {
// variable is defined and not null
} else {
// else do this
}
聽起來你正在對物體進行財產檢查! 如果要檢查屬性是否存在(但除了truthy值之外可以是null或0之類的值), in運算符可以產生一些很好的語法。
var foo = { bar: 1234, baz: null };
console.log("bar in foo:", "bar" in foo); // true
console.log("baz in foo:", "baz" in foo); // true
console.log("otherProp in foo:", "otherProp" in foo) // false
console.log("__proto__ in foo:", "__proto__" in foo) // true
如您所見, __ proto__屬性將被拋出此處。 對於所有繼承的屬性都是如此。 為了進一步閱讀,我建議使用MDN頁面:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
我在這里看到了很多選項,但我想說,在某些情況下,代碼將不會按照預期的方式執行,或者會引發錯誤。 如果您想要一種安全的方式,請使用try
catch
:
try { I_dont_exist } catch (err) { console.log('Variable is not defined') }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.