“Function Overloading” in JavaScript
JavaScript do not have function overloading because functions are objects, this is something we can alternatively do about it
JavaScript do not have function overloading because functions are objects, this is something we can alternatively do about it
changing the data inside the object is called modifying the internal state of the object, the state(data) is changed, but memory address has not changed
an object whose internal state can be changed
an object whose internal state can not be changed
“arguments” is another keyword JavaScript engine create during the creation of execution context, arguments contains a list of all the values of all the parameters that passed to a function.
argument is an array-like object, it doesn’t has all the features of JavaScript array
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function MyFunc(arg1, arg2, arg3){ console.log(arguments); // [] console.log(typeof(arguments)); // Object if(arguments.length === 0){ return; } console.log(arguments[0]); } MyFunc(); |
“arguments” will become deprecated in the future, the new thing is called spread
1 2 3 |
function myFunc(arg1, arg2, arg3, ...other){ // avaliable in the future... } |
variables are memory references, not equal to the object but reference(alias) the object at memory space.
Find out memory address referenced: using id()
after we created a object in memory, python keep track of the number of the references we have to the object, as soon as the count goes to 0, python memory manager destroy object and reclaim the memory space
Find out reference count:
in the circumstance of circular reference, the reference count could not goes to 0(memory leak), need garbage collector to identify it
can be control programmatically using the gc module, turned on by default, beware to turn it off, for python < 3.4, if even one of the objects in the circular reference has a destructor, the destruction order may be important, but the GC does not know what order should be, so the circular reference objects will be marked as uncollectable and cause memory leak
python variable name has no references to any type, when we use type(), python looks up the object which is referenced and return the type of the object
JavaScript engine every time an execution context is created, that is every time a function is RUN, it gives a variable called “this” pointed to a different object depends on how the function is invoked(where the function is and how it been called)
1 2 3 4 5 6 7 8 9 10 11 |
function greeting(){ console.log(this) } greeting(); // Window var man = { greeting: function(){ console.log(this); } } man.greeting(); // Object { greeting: greeting() } |
Weird part
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
var man = { greeting: function(){ function someFunctionSitInObjectFunction(){ console.log(this); } someFunctionSitInObjectFunction(); } } man.greeting(); // Window /* ==== Solution ==== */ var man = { greeting: function(){ var self = this; // by references function someFunctionSitInObjectFunction(){ console.log(self); // look for self variable in scope chain } someFunctionSitInObjectFunction(); } } man.greeting(); // Object { greeting: greeting() } |
copying the value into two separate spots in memory
no copy of object is created, instead, two names or more point to same address
1 2 3 4 5 6 7 8 9 |
var a = { greeting: 'hello', }; var b = a; a.greeting = 'hola'; b.greeting === 'hola'; //true /* set up a new memory space and point b to if */ b = { greeting: '你好' }; a.greeting != b.greeting; //true |