when you assign an object (not a primitive) to a variable (or a property of
another object) then you are assigning a reference to the object. The
variable only stores the reference to the object. The object is stored in
memory, and can be referenced by many variables, as im sure you know. If
you use methods on the referenced object then you will mutate it (possibly
- depending on the method). So, in the case of using push/pop on an array,
they mutate the array, by adding/removing items.

This is the same for any instance of object (instance of class if you want
to think classically), that you create. You can mutate it however you want,
you can assign new properties/methods (objects are dynamic), or mutate it
in any way you want. aslong as you are working ON the reference.

eg:

var obj = {};
obj.one = 1; // mutating obj
obj.test = function() {} // mutating obj

var obj2 = obj; // another reference to the 'obj' object

obj = {} // creating a new empty object and assigning a reference to it
under the variable 'obj'

obj2.one // still exists and == 1, as the object created initially still
exists and is referenced by the obj2 variable.

obj2 = 2 // reassigning the variable obj2 to a primitive value of '2'

// now there are no references to the object that was previously assigned
to obj1 and obj2, therefore it will be cleaned up by garbage collection
(removed from memory)


hope that helps

On 1 December 2014 at 21:29, Puritan Paul <[email protected]> wrote:

> Right, I see what you mean.  I definitely thought that type of assignment
> was by reference.
>
> Can you tell me then why when I push or pop a new element to the services,
> my controller reference is updated accordingly?  Are arrays passed by
> reference and objects not?
>
> As I typed that I realized I could easily search and find out myself…
>
> Thanks for the help!
>
>
>
> On Dec 1, 2014, at 8:19 PM, Tony pee <[email protected]> wrote:
>
> The problem is with your understanding of how references work in
> javascript i feel. In a small example:
>
> var obj = { prop: 123 }; an object with a property
> var ref = obj.prop;
>
> ref = 456; // this will NOT mean that  obj.prop == 456, it means that ref
> is equal to a primitive value of 456. obj.prop is untouched.
>
> In your example, when you do lines like this:
>
> $scope.model.allModel = MainSvc.getModel();
>
> you set on $scope.model (a different object) a reference to the object
> which is ALSO stored in your model class. when you change the value of
> $scope.model you just change what that reference is pointing to. The object
> referenced in your service is untouched. You should try directly
> referencing the value stored on the model. And you dont really want to set
> it on the controller, otherwise you'd need to also update it on the service
> to make it share between controllers (original purpose). So its better to
> prove that it can be shared:
>
> http://plnkr.co/edit/Q054Lw0Mi80LRnCBKlg6?p=preview
>
> I changed the style to a more class like style. And kept your getters and
> setters, altho they arent really needed (see the direct method works too)
>
>
>
> On 1 December 2014 at 18:05, Jonathan Price <[email protected]> wrote:
>
>> I've got a service that has a collection of things.  It also has an
>> individual thing of the same type.  I'll use a service like this to pass
>> around between controllers when I have a collection of things that I'll
>> individually want to update.    Imaging a collection of say books.  I can
>> display the collection through a table, click on one to edit, update it and
>> reload the entire collection..
>>
>> I've got a plunker here that shows what I'm confused by:
>>
>> http://plnkr.co/edit/P2smRGGAjEIHbZyAAKKR?p=preview
>>
>> I don't understand why my controller's scope isn't staying directly tied
>> to the service values?  I thought they became associated by reference when
>> I set them equal to one another in my controller?
>>
>> Thanks!
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "AngularJS" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/angular.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Tony Polinelli
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "AngularJS" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/angular/DXIj_XsrqDM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Tony Polinelli

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to