最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I shorthand checking for undefined? - Stack Overflow

programmeradmin1浏览0评论

I have code like this:

var process = function(next){
    //do stuff
    if(typeof next != 'undefined') { next(a, b, c, d, e); }

}

I'm so sick of typing typeof everywhere. Is there a global function I can write that handles checking for undefined as well as all the arguments?

For example:

_call = function(next){
    if(typeof next != 'undefined') next();
};

The above example doesn't work, by the way. Because node throws an error when I do this:

_call(next('hello', 'world')); //ERROR! next is undefined

So maybe I can do this?

_call(next, argument1, argument2, ... ) 

I have code like this:

var process = function(next){
    //do stuff
    if(typeof next != 'undefined') { next(a, b, c, d, e); }

}

I'm so sick of typing typeof everywhere. Is there a global function I can write that handles checking for undefined as well as all the arguments?

For example:

_call = function(next){
    if(typeof next != 'undefined') next();
};

The above example doesn't work, by the way. Because node throws an error when I do this:

_call(next('hello', 'world')); //ERROR! next is undefined

So maybe I can do this?

_call(next, argument1, argument2, ... ) 
Share Improve this question edited Aug 31, 2016 at 5:51 TIMEX asked Aug 31, 2016 at 5:43 TIMEXTIMEX 273k368 gold badges802 silver badges1.1k bronze badges 3
  • Actually you shouldn't be checking for it not to be undefined, you should be checking for it to be function. – Bergi Commented Aug 31, 2016 at 5:58
  • You're looking for function _call(fn, ...args) { if (typeof fn == "function") return fn(...args); } (or fn.apply(null, Array.prototype.slice.call(arguments, 1)) in ES5) – Bergi Commented Aug 31, 2016 at 5:59
  • @Bergi yes that's what I want. Can you write it as an official answer? – TIMEX Commented Aug 31, 2016 at 6:03
Add a ment  | 

5 Answers 5

Reset to default 2

Is there a builtin function that handles checking for undefined as well as all the arguments?

No, but you can write one yourself.

So maybe I can do this? _call(next, argument1, argument2, ... )

Yes:

function _call(fn, ...args) {
    if (typeof fn == "function") return fn(...args);
}

(using ES6 rest & spread syntax, in ES5 it would be fn.apply(null, Array.prototype.slice.call(arguments, 1))

This is a bit of a hack but you might be able to use default arguments

(function(next=()=>{}){
    //do stuff
    next(a, b, c, d, e); 
})();

So if it is not called with an argument, next will be an empty function that doesn't do anything

You don't need the typeof at all. The terminology in this case is slightly strange but here is the explanation:

var v; // initialize variable v
if (v) {} // works although v has type "undefined"
if (notInitialized) {} // ReferenceError: notDefined is not defined

And it is the same when you have a function with parameters. The arguments are always initialized but might have the type undefined.

As a result, you can use either

var process = function(next){
    //do stuff
    if (next) { next(a, b, c, d, e); }
}

or even

var process = function(next){
    next && next(a, b, c, d, e);
}

However, before actually calling next it might be a good approach to check whether it is actually a function.

If you are using ES6, you might also be able to use default parameters in case these work with your use case.

use this

_call = function(next){
    if(next && next != 'undefined' && next != 'null') next();
};

It should be undefined and throws an error. Because you call a function named next and pass as argument to _call.

This is the right one :

_call(function('hello', 'world'){//do dtuff});

And

_call = function(next){
    if(typeof next === 'function') next();
};
发布评论

评论列表(0)

  1. 暂无评论