Javascript define variable notation question -
saw js notation today i've never seen before, bear me if common knows.
var cookiepath = cookiepath || '';
now, saying, if variable named cookiepath exists, set cookiepath or if doesn't, set ''?
the cookiepath
variable being declared, initialized.
the var
statement doesn't make harm if identifier declared in current lexical scope.
if cookiepath
wasn't declared yet, var
statement, before run-time, initialize variable undefined
.
after that, in run-time, assignment made, if it's value falsy (other null
, undefined
, empty string, 0
, nan
, or false
) it's set empty string.
keep in mind have access cookiepath
variable in local scope.
consider following example:
var cookiepath = 'outer'; (function () { var cookiepath = cookiepath || ""; alert(cookiepath); // alerts empty string, not "outer" })();
in above example, have global cookiepath
variable, on global scope, when function executed, local cookiepath
variable declared on scope of function, , shadows value of outer scope, , behavior noticeable before var
statement in function, e.g.:
var foo = 'foo'; (function () { alert(foo); // undefined, not 'foo' outer scope var foo; // undefined })();
Comments
Post a Comment