Types as Arguments

In notebook:
FrontEndMasters Advanced JS fundamentals
Created at:
2015-10-08
Updated:
2015-10-08
Tags:
Fundamentals JavaScript
Learn JS Fundamentals to jQuery & Pure DOM Scriptingwhenever a function is created in memory it has a PROTOTYPE property that points to an object
  var sum = function(x,y){
    return (++x) + (++y.val);
};
a = 1;
b = {val:2};
c = sum(a,b);
inside ​sum(a,b)​ the ​x​ is a primitive so it's passed by value (it will pass a copy ​a​) but ​y​ will be passed by reference so it will point to ​b​ 
this is why ​a​ will stay as 1 but b.val​ will increment to 2.

note: in JavaScript primitives cannot be mutated - if you change it will take another place in memory
Call objects know about their parent call objects - this is how closures work
Call objects are objects (like ​window​ or ​global​) (it holds variables, ​this​ reference, etc.)

Every time you call a function a new call object is created
So if a function returns another function and it's called several times, every time a new function is created. This is wgy ​c2()​ fill be 1 here:
  var counter = function () {
    var count = 0;
    return function () {
        return ++count;
    }
}
var ca1 = counter();
c1(); // 1
c1(); // 2
var c2 = counter();
c2(); // 1