Blog

ExtJS and Sencha Touch Themes and Templates

JavaScript References

Mar 17, 2009 | English | By | 3 Comments | Leer en Español

A reference is a pointer to the exact place where an object is found, in JavaScript this is a fundamental concept that we need to know and master.

JavaScript References
Author: Crysfel

I'm a software developer with 6+ years of experience, when I'm not developing software I may be writing a tutorial, you can follow me on twitter

Definition

Physically all the objects are stored in the memory and we can have access to them through a reference, which is contained in a variable. Multiple variables can have a reference to the same object and this object may contain references to other objects like strings, number, arrays, etc..

When multiple variables point to the same object and it is modified, the change will be reflected in all the variables that have a reference to the object. One example of this is:

//We create an empty object
var obj = {};

//We create a reference
var reference = obj;

//We add a property to the original object
obj.property = 1;

//The reference can have access to the property we created previously
console.debug('reference.property = '+reference.property);

The same principle applies to the arrays, even though they modify themselves through the method “push” the references will be affected. Let’s analyze the next example:

var array = ['Ext JS','Mootools','jQuery'];
var ref = array;

array.push('prototype');

console.debug(ref.length == array.length);
console.debug(ref);

Only references to objects

It is important to mention that in JavaScript the references only point to objects that are in memory not to the other references like the language C/C++. The next example shows this behavior:

//We create the original object
var obj1 = {property:'Original value'};
//We make a reference to the original object
var ref1 = obj1;

//obj1 points to a New object
obj1 = {property:'New Object!'};

//Ref1 points to the original object, therefore they're different
console.debug('same object = '+(obj1.property == ref1.property));
console.debug(obj1.property);
console.debug(ref1.property);

Concatenation

The strings are objects too and we have a reference to them through a variable; it is important to remember when we concatenate one or more strings the result will always be a new object. The next example shows that when we concatenate a text we create a new string and therefore the reference is pointing to the original string.

var str = 'Hello world!';
var refStr = str;

str += ' this is Crysfel';
console.debug('same string = '+(str === refStr));

If you have any questions or suggestions about this chapter, go ahead and ask them in the comments, I’d be glad to answer them.

3 Responses to “JavaScript References”

  • puran Apr 09, 2010

    i am having some misconception like in starting you explained that whatever we assign to object it will updated to reference variable but last 2 ex. its reverse
    please explain it.

  • ferrymulyanto Jun 26, 2011

    great,nice information, good learning javascript for beginner

  • lukasz Aug 02, 2011

    I have the same question as puran Apr 09, 2010 “at the begining you explained that whatever we assign to object it will updated to reference variable but last 2 ex. its reverse
    please explain it”

Leave a Reply







Updates

RSS

Subscribe to our feeds to receive updates of our newest posts and free tutorials.

Site search

Maybe we have what you need, would you like to search first?

Donations

Would you buy me a cup of coffee? I am sharing my knowledge and time with you, help this project grow. Thank you!