1

In Javascript The Good Parts, it states:

alt text

So I would expect the following code example to output 1001 since "objects are never copied but passed around by reference", so why does it output 0000?

var page_item = {
  id_code : 'welcome',
  title : 'Welcome',
  access_groups : {
      developer : '0010',
      administrator : '0100'
  }
};
page_item.access_groups.member = '0000';
var member = page_item.access_groups.member;
member = '1001';

$('p#test').html(page_item.access_groups.member); //should be "1001" but is "0000"

Added:

@Gareth @David, thanks, this is what I was trying to show in this example, works:

var page_item = {
  id_code : 'welcome',
  title : 'Welcome',
  access_groups : {
      developer : '0010',
      administrator : '0100'
  }
};
var page_item2 = page_item;
page_item2.access_groups.developer = '1001';

$('p#test').html(page_item.access_groups.developer); //is '1001'
2
  • This is the same in Python, and easily explained there (x.y = ... is a method call on x's __dict__, x = ... is not a method call). @Potential answerers: Is there a similar reasoning in JS? Commented Dec 14, 2010 at 15:49
  • @delnan - x.y = is not a method call in Javascript. (Well, it is possible in ECMAScript to define setter and getter methods for object properties but that's not the paradigm which caused the confusion in this question) Commented Dec 14, 2010 at 15:54

3 Answers 3

4

Don't think of pass-by-reference in the C++ context, because it's not the same.

var member = page_item.access_groups.member // Sets member to this value
member = '1001'; // Now sets it to another value

If there was a method on strings which changed them, then this:

member.removeLastLetter();

would alter page_item.access_groups.member. However, with your = you are changing the variable's reference, not the object it previously referenced

Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking about whether this is actually correct or not, and a key thing to note is that String is an object too. After I realized that, it made more sense to me.
1

Because page_item.access_groups.member is a string and not an Object.

1 Comment

Even if page_item.access_groups.member = {}, a following line var member = page_item.access_groups.member; member = {foo:1001} wouldn't update the page_item.access_groups.member object
0

This is probably getting bashed by JS-Gurus but basically it goes down like this:

Objects are passed by reference.
Strings (numbers, etc... basically 1 dimensional variables) are passed by value.

I did try and understand the lengthy explanations on data types but I seriously needed some work done and haven't gotten time to look at it more closely.

1 Comment

PBR and PBV are basically myths in javascript. The main point is, if you have a reference to anything (string or otherwise) in a variable var x, as soon as you write x = ... you are discarding your initial reference and creating a new one, as opposed to performing the an operation on the value you had previously referenced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.