Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. reference type vs. primitive type

reference type vs. primitive type

Scheduled Pinned Locked Moved JavaScript
questionjavascriptvisual-studio
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    ThetaClear
    wrote on last edited by
    #1

    Hi, I know that objects in javascript, like in many other languages, holds the reference to the object itself and primitive types like number bool holds the actual value. I have something like this:

    function manager()
    {

    var anObject = {prop1: 1, prop2: 2};

    .
    .
    .

    return
    {
    getMyObject: function(){
    
    return anObject;
    }
    

    }

    My question is why when I do something like this:

    var m = new manager();
    var o = m.getMyObject();
    o = {a: 10, b: 20};

    The actual value in 'o' which is a reference type is not changed when I do this:

    var o2 = m.getMyObject();

    what is actually set in the variable 'o'? and how can I make it persist in the above

    Richard DeemingR T 2 Replies Last reply
    0
    • T ThetaClear

      Hi, I know that objects in javascript, like in many other languages, holds the reference to the object itself and primitive types like number bool holds the actual value. I have something like this:

      function manager()
      {

      var anObject = {prop1: 1, prop2: 2};

      .
      .
      .

      return
      {
      getMyObject: function(){
      
      return anObject;
      }
      

      }

      My question is why when I do something like this:

      var m = new manager();
      var o = m.getMyObject();
      o = {a: 10, b: 20};

      The actual value in 'o' which is a reference type is not changed when I do this:

      var o2 = m.getMyObject();

      what is actually set in the variable 'o'? and how can I make it persist in the above

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Your code currently does the following:

      1. Store the object returned from getMyObject in a variable called o;
      2. Store a new object in the o variable;
      3. Store the object returned from getMyObject in a variable called o2;

      It should be fairly obvious why o and o2 do not point to the same object - step 2 makes o point to a new, different object. If you want your changes to the object to be persisted for future calls of the getMyObject function, you need to change the properties of the object returned from that function:

      var m = new manager();
      var o = m.getMyObject();

      o.prop1 = 10;
      o.prop2 = 20;

      var o2 = m.getMyObject();
      // o2 contains { prop1: 10, prop2: 20 }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      T U 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        Your code currently does the following:

        1. Store the object returned from getMyObject in a variable called o;
        2. Store a new object in the o variable;
        3. Store the object returned from getMyObject in a variable called o2;

        It should be fairly obvious why o and o2 do not point to the same object - step 2 makes o point to a new, different object. If you want your changes to the object to be persisted for future calls of the getMyObject function, you need to change the properties of the object returned from that function:

        var m = new manager();
        var o = m.getMyObject();

        o.prop1 = 10;
        o.prop2 = 20;

        var o2 = m.getMyObject();
        // o2 contains { prop1: 10, prop2: 20 }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        T Offline
        T Offline
        ThetaClear
        wrote on last edited by
        #3

        Thanks for your reply Richard, I understood the sequence but there's still something not clear for me. A variable which holds a reference, basically holds a pointer to an object. If I set variable 'a' which holds a reference to object1 to a new variable called 'b', both of them will have a pointer that should point to the same place i.e. object1. Maybe I'm confusing it with a language like c# but if I set a new object to variable 'a' called 'object2', I'm creating a new pointer to that new object. I expect the place in memory where the original pointer which pointed to 'object1' will change and point to object2 for both 'a' and 'b'. But I see that's not happening. 'a' points to the new object while 'b' still points to the older one.

        Richard DeemingR 1 Reply Last reply
        0
        • T ThetaClear

          Thanks for your reply Richard, I understood the sequence but there's still something not clear for me. A variable which holds a reference, basically holds a pointer to an object. If I set variable 'a' which holds a reference to object1 to a new variable called 'b', both of them will have a pointer that should point to the same place i.e. object1. Maybe I'm confusing it with a language like c# but if I set a new object to variable 'a' called 'object2', I'm creating a new pointer to that new object. I expect the place in memory where the original pointer which pointed to 'object1' will change and point to object2 for both 'a' and 'b'. But I see that's not happening. 'a' points to the new object while 'b' still points to the older one.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          No. If variables a and b point to object1, and you change variable a to point to object2, variable b will not be affected. If you're more comfortable with C#, consider the following contrived example:

          string a = string.Empty;
          string b = string.Empty;

          a = "Hello";

          What value do you think will be in b after you run this code? If the language followed your idea, since both variables start out pointing to the same string, when you change a to point to the string "Hello", it would automatically change b as well. That would make it virtually impossible to write a program, since all variables and fields of the same type would automatically be pointing to the same instance of that type. Think of it like postal addresses: person a lives at address1, and person b also lives at address1. If you change the street-name for address1, that change affects both people. If person a moves to address2, that doesn't necessarily mean that person b moves as well.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          T 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            No. If variables a and b point to object1, and you change variable a to point to object2, variable b will not be affected. If you're more comfortable with C#, consider the following contrived example:

            string a = string.Empty;
            string b = string.Empty;

            a = "Hello";

            What value do you think will be in b after you run this code? If the language followed your idea, since both variables start out pointing to the same string, when you change a to point to the string "Hello", it would automatically change b as well. That would make it virtually impossible to write a program, since all variables and fields of the same type would automatically be pointing to the same instance of that type. Think of it like postal addresses: person a lives at address1, and person b also lives at address1. If you change the street-name for address1, that change affects both people. If person a moves to address2, that doesn't necessarily mean that person b moves as well.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            T Offline
            T Offline
            ThetaClear
            wrote on last edited by
            #5

            Great, that was a good explanation. Thanks Richard!

            1 Reply Last reply
            0
            • T ThetaClear

              Hi, I know that objects in javascript, like in many other languages, holds the reference to the object itself and primitive types like number bool holds the actual value. I have something like this:

              function manager()
              {

              var anObject = {prop1: 1, prop2: 2};

              .
              .
              .

              return
              {
              getMyObject: function(){
              
              return anObject;
              }
              

              }

              My question is why when I do something like this:

              var m = new manager();
              var o = m.getMyObject();
              o = {a: 10, b: 20};

              The actual value in 'o' which is a reference type is not changed when I do this:

              var o2 = m.getMyObject();

              what is actually set in the variable 'o'? and how can I make it persist in the above

              T Offline
              T Offline
              twseitex
              wrote on last edited by
              #6

              constructor of "ype "m" is function manager(){...} m = new constructor (...) m is a pointer ! e.g. new Function(...) creates a pointer of a function new Function() is a member of Javascript predefines types (predefined constructor) so manager() runs like new Function() ? function manager() { var anObject = {prop1: 1, prop2: 2}; // local var !!!! // does not exists after the end of constructor // use global var ! . . . return { getMyObject: function(){ return anObject; } // local function !!!! // does not exists after the end of constructor // use global var to save the pointer. }

              1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Your code currently does the following:

                1. Store the object returned from getMyObject in a variable called o;
                2. Store a new object in the o variable;
                3. Store the object returned from getMyObject in a variable called o2;

                It should be fairly obvious why o and o2 do not point to the same object - step 2 makes o point to a new, different object. If you want your changes to the object to be persisted for future calls of the getMyObject function, you need to change the properties of the object returned from that function:

                var m = new manager();
                var o = m.getMyObject();

                o.prop1 = 10;
                o.prop2 = 20;

                var o2 = m.getMyObject();
                // o2 contains { prop1: 10, prop2: 20 }


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                U Offline
                U Offline
                User 11056480
                wrote on last edited by
                #7

                have a look at dis bugga !!! CLT20 2014
                CLT20 2014 Live
                CLT20 2014 live streaming
                CLT20 2014 matches
                CLT20 2014 Teams
                CLT20 2014 Highlights

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups