Weird Code - Dynamic properties in primitive and reference variables in javascript (JS) -
Weird Code - Dynamic properties in primitive and reference variables in javascript (JS) -
i'm working through learning javascript , reading chapter 4 of professional javascript web developers. on p. 86
primitive values can’t have properties added them though attempting won’t cause error. here’s example:
var name = “nicholas”; name.age = 27; alert(name.age); //undefined
they javascript not treat strings objects, other languages do. wanted see if print out name.age
.
i tried
var name = "nicholas"; name.age = 27, alert(name.age);
and got undefined.
trying
var name = "nicholas"; name.age = 27 & alert(name.age);
also gave undefined.
but,
var name = "nicholas"; alert(name.age = 27);
gives 27!
with regards text's original example, author says
"here property called age defined on string name , assigned value of 27. on next line, however, property gone. reference values can have properties defined dynamically later use."
what going on comma separated assignment , function phone call - knew utilize commas separate variable assignments, can function calls? limits comma in javascript? how&
operator work chaining code snippets? operator , should used for? why did lastly illustration work when other ones wouldn't? have scope? edit: jlrishe , sirreal. didn't understand jlrishe's reply until reading sirreal's, recommend reading both!
really, reply 3 of questions has how ,
, &
, =
operators defined, , has nil properties on primitive values.
your sec illustration example of comma operator, , different comma used in variable declarations. evaluates each of operands 1 @ time left right, , ultimate value of look value of lastly operand.
the &
operator evaluates 2 operands left right , produces result of applying bitwise and
them. alert()
executing side effect of this. look 27 & alert(name.age)
evaluates 0
, , assigned name.age
(but doesn't anything). can see similar behavior with:
name.age = 27 * alert(name.age);
or
name.age = 27 - alert(name.age);
or several other operators.
what observed in lastly illustration well-defined behavior of =
operator. performs assignment , produces value of right-hand side. assignment doesn't happen in case, reasons quoted, evaluates value of right-hand nonetheless, , passed alert()
function.
javascript
Comments
Post a Comment