Learning Ext JS 3

JavaScript References Más videos

Descripción del tema

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.

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.

Te gustaría recibir más tutoriales como este en tu correo?

Este tutorial pertenece al curso Learning Ext JS 3, te recomiendo revises el resto de los tutoriales ya que están en secuencia de menor a mayor complejidad.

Si deseas recibir más tutoriales como este en tu correo te recomiendo registrarte al curso, si ya eres miembro solo identifícate y registrate al curso, si no eres miembro te puedes registrar gratuitamente!

Si no gustas registrarte en este momento no es necesario! Aún así puedes recibir los nuevos tutoriales en tu correo! Jamás te enviaremos Spam y puedes cancelar tu suscripción en cualquier momento.

¿Olvidaste tu contraseña?

3Comentarios

  • Avatar-7 puran 09/04/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.

    • Avatar-5 ferrymulyanto 27/06/2011

      great,nice information, good learning javascript for beginner

      • Avatar-7 lukasz 02/08/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"

        Instructor del curso

        Crysfel3

        Autor: Crysfel Villa

        Software engineer with more than 7 years of experience in web development.

        Descarga Código Fuente

        Regístrate a este curso

        Este tutorial pertenece al curso Learning Ext JS 3, revisa todos los tutoriales que tenemos en este mismo curso ya que están en secuencia y van de lo más sencillo a lo más complicado.

        Tendrás acceso a descargar los videos, códigos y material adicional.

        Podrás resolver los ejercicios incluidos en el curso así como los Quizzes.

        Llevarás un registro de tu avance.