var Blah = Blah || {};
or
if ((typeof Blah) == 'undefined') {
var Blah = {};
}
Is there a difference or both do the same thing?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is a difference. The first assigns
BlahtoBlah, but ifBlahis a falsy value (one which typecasts tofalsein a boolean context, such asNaN,undefined,0and''), it’ll setBlahto an empty object.The second only sets
Blahto an empty object if it is exactlyundefined, as the only value with thetypeoftypeundefinedisundefined.Also, you are creating just objects, not namespaces. Even the concept of namespaces in JavaScript is iffy at best; most of the time it is done by creating a self-calling anonymous function that exports some things into a global object and not others.