Thats because you're not passing objects, but references (pointers).

// 'a' is a reference/pointer to {}
var a = {};

// obj is a localized reference/pointer to {} NOT 'a'
function mutate( obj ) {
     // local 'obj' reference is overwritten, NOT {}
    obj = function(){ alert('hey') }
}

Consider the following example:

var a = {};
var b = a;

Both 'a' and 'b' are pointers to an object.
'b' does NOT refer/point to 'a', 'a' just happens to be the first
declared reference/pointer.

The real Objects themselves are actually anonymous and variables
simply point/refer to them.



On 13 dec, 22:57, Rajat <[email protected]> wrote:
> Hi,
>
> I was trying something today when this occurred to me :
>
> var a = {};
>
> > function mutate(obj){
>
>     obj = function(){
>
>         alert('hey');
>
>     };
>
> }
> > mutate(a);
>
> a();
>
> Firebug throws the following error:
>
> > TypeError: a is not a function.
>
> I want to understand why we can change the passed object inside mutate by
> lets say adding more properties to it but not this way where I am redefining
> it as a function object.
>
> I hope my question is clear.
>
> Thanks,
>
> Rajat Mittalhttp://www.lifeinafolder.com

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to