如何检查在Node.js中定义了一个变量?

时间:2022-04-17 16:09:43

I am working on a program in node.js which is actually js.

我正在node开发一个程序。实际上是。

I have a variable :

我有一个变量:

var query = azure.TableQuery...

looks this line of the code is not executing some times.

看起来这行代码有些时候没有执行。

my question is :

我的问题是:

How can I do a condition like:

我怎样才能做到这样的条件:

if this variable is defined do this.
else do this.

I cannot do in js (query!= null)

我不能用js(查询!= null)

I want to see if this variable is defined do some thing. how to do this

我想看看这个变量是否被定义了做一些事情。如何做到这一点

6 个解决方案

#1


58  

if ( typeof query !== 'undefined' && query )
{
  //do stuff if query is defined and not null
}
else
{

}

#2


17  

Determine if property is existing (but is not a falsy value):

确定属性是否存在(但不是假值):

if (typeof query !== 'undefined' && query !== null){
   doStuff();
}

Usually using

通常使用

if (query){
   doStuff();
}

is sufficient. Please note that:

是充分的。请注意:

if (!query){
   doStuff();
}

doStuff() will execute even if query was an existing variable with falsy value (0, false, undefined or null)

doStuff()将执行,即使查询是一个有错误值的现有变量(0、false、未定义或null)

Btw, there's a sexy coffeescript way of doing this:

顺便说一句,有一种性感的写真方式:

if object?.property? then doStuff()

which compiles to:

它编译:

if ((typeof object !== "undefined" && object !== null ? object.property : void 0) != null) 

{
  doStuff();
}

#3


9  

For me, an expression like

对我来说,像这样的表达

if (typeof query !== 'undefined' && query !== null){
   // do stuff
}

is more complicated than I want for how often I want to use it. That is, testing if a variable is defined/null is something I do frequently. I want such a test to be simple. To resolve this, I first tried to define the above code as a function, but node just gives me a syntax error, telling me the parameter to the function call is undefined. Not useful! So, searching about and working on this bit, I found a solution. Not for everyone perhaps. My solution involves using Sweet.js to define a macro. Here's how I did it:

它比我想要的要复杂得多,因为我想经常使用它。也就是说,我经常测试一个变量是否定义/null。我想要一个简单的测试。为了解决这个问题,我首先尝试将上面的代码定义为函数,但是node只是给了我一个语法错误,告诉我函数调用的参数没有定义。不是有用的!所以,在搜索和研究这一点的过程中,我找到了一个解决方案。也许对每个人来说都不是。我的解决办法是用甜的。定义一个宏。我是这样做的:

Here's the macro (filename: macro.sjs):

这是宏(文件名: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;

Here's an example of usage of the macro (in example.sjs):

下面是一个使用宏的例子(在example.sjs中):

function Foobar() {
    var self = this;

    self.x = 10;

    console.log(isDefined(y)); // false
    console.log(isDefined(self.x)); // true
}

module.exports = Foobar;

And here's the main node file:

这里是主节点文件:

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();

A downside of this, aside from having to install Sweet, setup the macro, and load Sweet in your code, is that it can complicate error reporting in Node. It adds a second layer of parsing. Haven't worked with this much yet, so shall see how it goes first hand. I like Sweet though and I miss macros so will try to stick with it!

除了必须安装Sweet、设置宏和在代码中加载Sweet之外,还有一个缺点就是它会使Node中的错误报告复杂化。它添加了第二层解析。还没有做过这么多,所以我们先来看一下进展如何。我喜欢甜食,但我想念宏,所以我会坚持下去!

#4


2  

If your variable is not declared nor defined:

如果您的变量未被声明或定义:

if ( typeof query !== 'undefined' ) { ... }

If your variable is declared but undefined. (assuming the case here is that the variable might not be defined but it can be any other falsy value like false or "")

如果您的变量是声明的,但没有定义。(假设这里变量可能没有定义,但它可以是任何其他的假值,比如false或"" ")

if ( query ) { ... }

If your variable is declared but can be undefined or null:

如果您的变量已声明,但可以是无定义的或空的:

if ( query != null ) { ... } // undefined == null

#5


0  

It sounds like you're doing property checking on an object! If you want to check a property exists (but can be values such as null or 0 in addition to truthy values), the in operator can make for some nice syntax.

这听起来像是在对一个对象进行属性检查!如果您想要检查一个属性(但可以是null或0这样的值,除了truthy的值),那么在操作符中可以使用一些很好的语法。

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

As you can see, the __proto__ property is going to be thrown here. This is true for all inherited properties. For further reading, I'd recommend the MDN page:

如您所见,__proto__属性将被抛出这里。对所有继承属性都是如此。为了进一步阅读,我推荐MDN页面:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

#6


0  

For easy tasks I often simply do it like:

对于简单的任务,我通常会这样做:

var undef;

// Fails on undefined variables
if (query !== undef) {
    // variable is defined
} else {
    // else do this
}

Or if you simply want to check for a nulled value too..

或者如果你只是想检查一个空值。

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
}

#1


58  

if ( typeof query !== 'undefined' && query )
{
  //do stuff if query is defined and not null
}
else
{

}

#2


17  

Determine if property is existing (but is not a falsy value):

确定属性是否存在(但不是假值):

if (typeof query !== 'undefined' && query !== null){
   doStuff();
}

Usually using

通常使用

if (query){
   doStuff();
}

is sufficient. Please note that:

是充分的。请注意:

if (!query){
   doStuff();
}

doStuff() will execute even if query was an existing variable with falsy value (0, false, undefined or null)

doStuff()将执行,即使查询是一个有错误值的现有变量(0、false、未定义或null)

Btw, there's a sexy coffeescript way of doing this:

顺便说一句,有一种性感的写真方式:

if object?.property? then doStuff()

which compiles to:

它编译:

if ((typeof object !== "undefined" && object !== null ? object.property : void 0) != null) 

{
  doStuff();
}

#3


9  

For me, an expression like

对我来说,像这样的表达

if (typeof query !== 'undefined' && query !== null){
   // do stuff
}

is more complicated than I want for how often I want to use it. That is, testing if a variable is defined/null is something I do frequently. I want such a test to be simple. To resolve this, I first tried to define the above code as a function, but node just gives me a syntax error, telling me the parameter to the function call is undefined. Not useful! So, searching about and working on this bit, I found a solution. Not for everyone perhaps. My solution involves using Sweet.js to define a macro. Here's how I did it:

它比我想要的要复杂得多,因为我想经常使用它。也就是说,我经常测试一个变量是否定义/null。我想要一个简单的测试。为了解决这个问题,我首先尝试将上面的代码定义为函数,但是node只是给了我一个语法错误,告诉我函数调用的参数没有定义。不是有用的!所以,在搜索和研究这一点的过程中,我找到了一个解决方案。也许对每个人来说都不是。我的解决办法是用甜的。定义一个宏。我是这样做的:

Here's the macro (filename: macro.sjs):

这是宏(文件名: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;

Here's an example of usage of the macro (in example.sjs):

下面是一个使用宏的例子(在example.sjs中):

function Foobar() {
    var self = this;

    self.x = 10;

    console.log(isDefined(y)); // false
    console.log(isDefined(self.x)); // true
}

module.exports = Foobar;

And here's the main node file:

这里是主节点文件:

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();

A downside of this, aside from having to install Sweet, setup the macro, and load Sweet in your code, is that it can complicate error reporting in Node. It adds a second layer of parsing. Haven't worked with this much yet, so shall see how it goes first hand. I like Sweet though and I miss macros so will try to stick with it!

除了必须安装Sweet、设置宏和在代码中加载Sweet之外,还有一个缺点就是它会使Node中的错误报告复杂化。它添加了第二层解析。还没有做过这么多,所以我们先来看一下进展如何。我喜欢甜食,但我想念宏,所以我会坚持下去!

#4


2  

If your variable is not declared nor defined:

如果您的变量未被声明或定义:

if ( typeof query !== 'undefined' ) { ... }

If your variable is declared but undefined. (assuming the case here is that the variable might not be defined but it can be any other falsy value like false or "")

如果您的变量是声明的,但没有定义。(假设这里变量可能没有定义,但它可以是任何其他的假值,比如false或"" ")

if ( query ) { ... }

If your variable is declared but can be undefined or null:

如果您的变量已声明,但可以是无定义的或空的:

if ( query != null ) { ... } // undefined == null

#5


0  

It sounds like you're doing property checking on an object! If you want to check a property exists (but can be values such as null or 0 in addition to truthy values), the in operator can make for some nice syntax.

这听起来像是在对一个对象进行属性检查!如果您想要检查一个属性(但可以是null或0这样的值,除了truthy的值),那么在操作符中可以使用一些很好的语法。

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

As you can see, the __proto__ property is going to be thrown here. This is true for all inherited properties. For further reading, I'd recommend the MDN page:

如您所见,__proto__属性将被抛出这里。对所有继承属性都是如此。为了进一步阅读,我推荐MDN页面:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

#6


0  

For easy tasks I often simply do it like:

对于简单的任务,我通常会这样做:

var undef;

// Fails on undefined variables
if (query !== undef) {
    // variable is defined
} else {
    // else do this
}

Or if you simply want to check for a nulled value too..

或者如果你只是想检查一个空值。

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
}

相关文章