admin管理员组

文章数量:1295332

assuming x is an object... Is there any benefit of doing:

 if (typeof x.foo != "undefined")

vs. doing

 if (x.foo)

?

This question came up as I was reading this blog post: /

In his example, he does:

function EventTarget(){
  this._listeners = {};
}

EventTarget.prototype = {

  constructor: EventTarget,

  addListener: function(type, listener){
    if (typeof this._listeners[type] == "undefined"){
        this._listeners[type] = [];
    }

    this._listeners[type].push(listener);

In this case this._listeners[type] will never be anything except an array-- so is it not true that it would be cleaner in this case to just do

addListener: function(type, listener){
    if (!this._listeners[type]){
        this._listeners[type] = [];
    }

    this._listeners[type].push(listener);

?

Also, as a side question, I don't get why he's doing:

EventTarget.prototype = {

  constructor: EventTarget

Isn't the constructor by default already set to EventTarget ('this') when you call new EventTarget() ?

assuming x is an object... Is there any benefit of doing:

 if (typeof x.foo != "undefined")

vs. doing

 if (x.foo)

?

This question came up as I was reading this blog post: http://www.nczonline/blog/2010/03/09/custom-events-in-javascript/

In his example, he does:

function EventTarget(){
  this._listeners = {};
}

EventTarget.prototype = {

  constructor: EventTarget,

  addListener: function(type, listener){
    if (typeof this._listeners[type] == "undefined"){
        this._listeners[type] = [];
    }

    this._listeners[type].push(listener);

In this case this._listeners[type] will never be anything except an array-- so is it not true that it would be cleaner in this case to just do

addListener: function(type, listener){
    if (!this._listeners[type]){
        this._listeners[type] = [];
    }

    this._listeners[type].push(listener);

?

Also, as a side question, I don't get why he's doing:

EventTarget.prototype = {

  constructor: EventTarget

Isn't the constructor by default already set to EventTarget ('this') when you call new EventTarget() ?

Share edited Apr 16, 2017 at 8:52 Cœur 38.7k26 gold badges204 silver badges277 bronze badges asked May 9, 2011 at 19:39 patrickpatrick 9,75213 gold badges65 silver badges126 bronze badges 4
  • There is also a third alternative: if (x.foo !== undefined). Notice the double = (equality without type coercion) – Dan Manastireanu Commented May 9, 2011 at 19:45
  • 1 @Dan: Although that breaks if some fool changes undefined. Yes, that's possible. – user395760 Commented May 9, 2011 at 19:50
  • @delnan True, but there are ways to guard even against that: (function(undefined){ ...undefined is really undefined here... }()) – Dan Manastireanu Commented May 9, 2011 at 19:53
  • Slaks? my question is totally answerable? Not sure what you mean.. – patrick Commented May 9, 2011 at 21:49
Add a ment  | 

2 Answers 2

Reset to default 10

Watch out for truthy values.

if (x.foo) will not run if x.foo is

  • false
  • null
  • undefined
  • ""
  • 0
  • NaN

Where as if (typeof x.foo !== "undefined") { only checks for whether the value is undefined

Alternative checks would be

if (x.foo !== undefined) { and if (x.foo !== void 0) {

Do be wary that undefined can be overwritten as a local variable

undefined = true is a valid statement and will break all your code. Of course you will never see this code in production so you don't really have to shield against it, it's just something to be wary of.

I personally tend to use

if (x.foo != null) {
    ...
}

a lot which checks for both null and undefined.

[[Edit]]

In your specific example it's either an Array or undefined so !foo is safe. personally I prefer to check specifically for undefined so that users know I only expect it to run when it's undefined rather then when it's null or false or "". This makes the code more explicit / self documenting.

As to

EventTarget.prototype = {

  constructor: EventTarget

If you overwrite EventTarget.prototype with a new object then the EventTarget.prototype.constructor property is lost and needs to be set again.

You do not need to set .constructor again if you just extend the prototype by calling EventTarget.prototype.method = ....

The first explicitly checks that x.foo is undefined, whereas if (x.foo) is checking to see if x.foo is truthy.

http://11heavens./falsy-and-truthy-in-javascript

本文标签: javascriptexplicit typeofquotundefinedquot check vs just checking for its existenceStack Overflow