Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts

    something = Object(myVar)

    Goal:

    var hello = "Hello";
    var bye = hello;
    hello = "Bye";
    alert(bye); //Shows "Bye" instead of "Hello"

    ###########################
    Note: I used to program in AS3 and the way to do that was
    var bye = Object(hello);

    However, in JS, it's not working What's the way to do it in JS?

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,134
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by IdkWhatsRc View Post
    However, in JS, it's not working What's the way to do it in JS?
    not working in what regard? do you want to see "Hello" (correct behaviour) or "Bye"?

    anyways, only objects are passed by reference, all others are passed by value (that’s why bye === "Hello").
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    not working in what regard? do you want to see "Hello" (correct behaviour) or "Bye"?

    anyways, only objects are passed by reference, all others are passed by value (that’s why bye === "Hello").
    Aren't variables objects in JS? But anyway, yes, my goal is to display "Bye" instead of "Hello".
    The thing that isn't working is the Object(myVar) trick that AS3 has.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,134
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by IdkWhatsRc View Post
    Aren't variables objects in JS?
    some are, others not. the ones who are not objects:
    - null
    - undefined
    - the String literals (e.g. "foo")
    - the Boolean literals (e.g. true)
    - the Number literals (e.g. 12)

    note, however, that the literals are temporarely converted to objects, if required (e.g. "foo".length)

    Quote Originally Posted by IdkWhatsRc View Post
    The thing that isn't working is the Object(myVar) trick that AS3 has.
    the trick would work in JS as well, if you could re-set the object’s value. just replacing one object with another doesn’t work.

    if you would like to use a reference, you can work around using an object.
    PHP Code:
    var = { text"foo" };
    var 
    x;
    x.text "bar";
    alert(y.text); // bar 
    Edit: var x = Object("foo") does indeed convert a literal into an object, but String objects are more or less read-only (i.e. they generate new strings). Contrary, the String(), Number() and Boolean() functions convert a value to the appropriate literal.
    Last edited by Dormilich; 06-03-2013 at 09:41 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Actually, variables are *NEVER* objects in JavaScript.

    A variable can only be a *REFERENCE* to an object. It can never *BE* an object.

    By the by, this is also true in Java and in C# and, excepting for auto variables, in C++.

    Looking at it from the machine-language side, this means that a variable can always be represented by a fixed size chunk of memory, no matter what the variable type. MSIE, for example, uses 16 bytes per variable, only. And when a variable references an object, this means that some of those 16 bytes (only 4 in a 32-bit OS environment) are a pointer to the actual object. Since an actual object could be thousands of bytes in size (or larger, esp. in the case of an array object), this enormously simplifies variable management.

    It is slightly strange that variables that represent strings, which clearly must indeed also be references to the actual (possibly quite long) string text are not treated the same as other object references. That is, it is not just the reference (the pointer) that is copied when you assign one string variable to another. But as Dormilich said, that is because strings are treated as read-only objects. You can never alter the content of a string. You can only create a new string that is some kind of derivation from the old one. And in the case of a simple assignment, this means that the entire string is copied.
    Last edited by Old Pedant; 06-03-2013 at 09:36 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •