开发者

Javascript: Control over undefined?

开发者 https://www.devze.com 2023-02-19 18:30 出处:网络
Maybe somehow extend try&catch or errors handling to throw errors on undefined catch, only in debug mode(flag on)?

Maybe somehow extend try&catch or errors handling to throw errors on undefined catch, only in debug mode(flag on)?

Because it will lessen my code and increase readability.

if (typeof foo != 'undefined') {
 /* Do something with foo */
}

Instead of using it right away.

Same with concatenating undefined variable to a string if there any solution.

'bar'+ ((typeof foo != 'undefined') ? foo : '') +'bar'

Instead of using it right away

'bar'+ foo +'bar'

Thanks :)

Update, some examples:

(function(o){
var s = {
    foo:''
}
$.extend(s,o);

console.log('testing'+ s.foo +'testing');
})();

(function(o){
console.log('testing'+ o.foo +'testing');
})({foo:''});

(function(o){
console.log('testing'+ (typeof o.foo != 'undefined' ? o.foo : '') +'testing');
})();
开发者_如何转开发
(function(o){
console.log('testing'+ o.foo +'testing');
})();


Your example:

(function(o){
    var s = {
        foo:''
    }
    $.extend(s,o);

    console.log('testing'+ s.foo +'testing');
})();

is the best way, in my opinion, to have a default value for an argument.

Other than that, I'm not quite sure what this question is asking. There is no way in JavaScript to extend the behavior of operators or keywords, such as try..catch. By extension, the default value of undefined can't be altered either. The best you can do is the above example.

0

精彩评论

暂无评论...
验证码 换一张
取 消