I see that in jQuery to be specific
var a= 'something' || function () {
}
or
var a = 'something' || { }
What does it mean? I kno开发者_StackOverfloww { } is an object json in javascript and am aware of anonymous functions in javascript but still cant figure out what does this means.
Thanks for the help in advance.
You most likely included a faulty example. Your code is valid, but would be pretty useless to write, since you are assigning a known string to a variable, which renders the following OR
statement useless.
What is common, is syntax like this:
function foo(bar) {
// set baz to the contents of bar
// or create an empty object if bar evaluates to false
var baz = bar || {};
}
This sort of "conditional assignment" is a common idiom in languages which takes advantage of short-circuiting of boolean operators (like "OR" in this case) to assign to a variable the value of the left-hand expression if it evaluates to a "true" value or the right-hand expression if not. Consider:
var nullOrFive = null || 5; // => 5
The left-hand side (null || five
) evaluates left-to-right testing each operand to the OR operator until it finds a "truthful" one, returning it. On the other hand:
var tenOrWhatever = 10 || someMethodThatIsNeverCalled(); // => 10
So in your examples, if "something" evaluates to true (which is anything but "undefined", "null", and zero in JavaScript), then the variable "a" will get its value, otherwise it gets the function (function() {...}
) or object literal ({}
).
when assigning a value to a variable, JavaScript is evaluating the given expression.
var s= s || {};
this means that if there already is a variable named s in the current scope, the newly created s variable is pointing to it. Else, if s variable is not defined in the current scope, or it points to a null reference, or to some other value evaluated to FALSE, the newly created s variable will point to a new object. This is useful when one is extending one object in multiple files:
File1
var globalNamespace = globalNamespace || {};
globalNamespace.someVariable = "some value;"
File2
var globalNamespace = globalNamespace || {};
globalNamespace.someFunction = function()
{
return this.someVariable;
};
In this way, one can extend the globalNamespace object without having to worry that the code is spitted in multiple files.
quoting the documentation on MDC,
Logical OR (||)
expr1 || expr2
Returns expr1 if it can be converted to true; otherwise, returns expr2.
Thus, when used with Boolean values, || returns true if either operand is true;
if both are false, returns false.
Additionaly, there is a short-circuit evaluation : As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules
so if expr1 can be evaluated to true, expr2 is not evaluated.
jQuery uses a lot of those short-circuit evaluation to define default values for variables. eg. var o = options || {}; will put the options in variable o ; but will make sure o is initialized to {} if options is undefined or null
精彩评论