I’m trying to figure out what does the empty {} mean.
var $sb = $sb || {};
Does this mean that varaible $sb’s value is either copied to itself or it is a function literal?
full context:
var $sb = $sb || {};
$sb.xxx = function() {
// code
}
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.
var a = {}is called the object literal notation. It’s a faster thanvar a = new Object()because it needs no scope resolution (ie you could have defined a constructor with the same name and therefor the JavaScript engine must do such a lookup).The pattern
var a = a || {};is used to avoid replacingain case you have already defineda. In this pattern, the or-operator:||functions as a coalescing operator. Ifaisnullorundefinedit will execute the expression at the right-hand of the statement:{}Using this pattern ensures you that
awill always be defined as anobjectand, in case it already exist, will not be overwritten.