Why my javascript variable referencing a function stay unchanged even after I overload the referenced function? -


i have problem of understanding how javascript works, , can't find correct explanation, perhaps me.

assuming following code :

// afunction references console.error var afunction = console.error;  // assign new property afunction // afunction references console.error have property afunction.callcount = 0;  afunction.hasownproperty("callcount");        //true console.error.hasownproperty("callcount");    //true  // overload console.error (why not purpose) console.error = function() {}  console.dir(afunction);     //vm807:2 function log() { [native code] } console.dir(console.error); //vm808:2 function () {} 

why afunction still references original console.error ?

thanks in advance help.

when set variable reference object (a function, in case), gets copy of reference value. subsequent changes other variables or object properties share reference value won't have effect on copy made.

it should clear what's going on here, right?

var = 2; var b = a; console.log(b); // 2 = 3; console.log(b); // still 2 

object references work same way:

var = { hello: "world" }; var b = a; console.log(b.hello); // world = { hello: "goodbye" }; console.log(b.hello); // still world 

variable a got new value, didn't affect b @ all. assignment operator = makes copies of values. there's no way make 1 javascript variable (object property) alias another.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -