{}" construct in javascript
Possible Duplicate:
What does “var FOO = FOO || {}” mean in Javascript?
Javascript - Can you add condition to variable declaration
I believe this has to do with scoping, and not redefining scope. I see a lot of this in popular javascript frameworks:
var something = something || {};
Is that to do with not accidentally redefining a top-level variable?
You are correct. If the variable already exists (ours or not), don't change it. If it doesn't exist, let's create a new one.
var something = something || {};
can be used inside of functions with parameters that may not be set
function doStuff( arg1, arg2, arg3 ) {
arg2 = arg2 || {};
arg3 = arg3 || arg1 + 2;
};
this makes the second and third arguments optional when you call the function doStuff
This creates a new object with local scope. If the outer something
is null, false, or undefined (or otherwise falsy), the new variable will be an empty object {}
.
上一篇: {}“的意思是在JavaScript中
下一篇: {}“在javascript中构造