Andy
Andy

Reputation: 19261

Object Property Renaming

I have a question regarding why the following returns inconsistent values

(function(ab, $, undefined ) {

    ​ab.cool = {
        nice: {
            funky: {
                sweet: false
            }    
        }
    };

    var reVal = ab.cool.nice.funky.sweet;
    reVal = true;

    console.log(reVal); //equals true
    console.log(ab.cool.nice.funky.sweet); //equals false

}( window.ab = window.ab || {}, jQuery ));​

I would have thought that both SHOULD return the same value true ?

Can someone please explain why this occurs ? Are they considered different properties ?

Upvotes: 0

Views: 321

Answers (2)

Ioannis Karadimas
Ioannis Karadimas

Reputation: 7906

What's happening is that the value of ab.cool.nice.funky.sweet gets copied to reVal. This would not happen if you tried to copy ab.cool.nice.funky to reVal, since both symbols would then reference the same object.

This differentiation is the product of treating value types, such as booleans or integers, differently than reference types which in javascript basically means objects.

Check this for more info: http://docstore.mik.ua/orelly/webprog/jscript/ch11_02.htm

Upvotes: 0

Julian Hollmann
Julian Hollmann

Reputation: 2912

You don't rename the variable, you assign the value to another.

it's like:

$foo = $bar = true;
$bar = false;

print $foo; // true
print $bar; // false

Upvotes: 3

Related Questions