buyerguy5 Posted January 20, 2012 Share Posted January 20, 2012 Hi, I am a novice at javascript and I am hoping the forum can help me out with a question:Given the following code:var x = 8;var y = x;var x = x+4;alert(y);The result is y=8Meaning, when you change the value of x (after the statement y=x), y doesn't correspond to x anymore.Is it possible to have y change with x all the time- is this doable?I thought in other languages, if you do y=x, it will permanently associate y with x so that every time x changes, so does y.How can this be done in javascript?Thanks Link to comment Share on other sites More sharing options...
Ingolme Posted January 20, 2012 Share Posted January 20, 2012 You're assigning only the value of x to y. If x changes afterwards y is not affected. The one thing you can do is some kind of function reference. var x = 8; var y = function() { return x; } x = x + 4; alert( y() ); People would call what you're asking for "syntactical sugar" Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.