Understanding Javascript: arguments, for-in, pass by value

In JavaScript, a function may be called with any number of arguments, no matter how many of them are listed.


function fn(a,b) {
console.log("a="+a+", b="+b)
}

fn(1) // a=1, b=undefined
fn(1,2) // a=1, b=2
fn(1,2,3) // a=1, b=2, extra argument is not listed

Arguments which are not provided become undefined. So there is no function overloading concept in javasript as there is in java,c++. There can be only one function named fn(), which is called for any given arguments.

You can  set default values like following if parameters are not passed.


function square(a) {
if(a ===undefined){
a=1;
}
return a*a;
}

console.log(square(2)); //4
console.log(square()); //1

function cube(a) {
a=a||1;
return a*a*a;
}

console.log(cube(2)); //8
console.log(cube()); //1

Javascript gives a keyword arguments which contains all the parameters passed to an function.


function fn(a,b) {
console.log(arguments);
console.log(arguments[0]);
console.log(arguments[1]);
console.log(arguments[2]);
}

fn(1,2,3);

arguments1

Note that it may looks like an array but is not an array, so all methods of array wont work on it.

for in loop

The for...in statement iterates over the enumerable properties of an object, in arbitrary order.


function Employee(fname,lname){
this.fname=fname;
this.lname=lname;
}

var john=new Employee("John","Doe");

for(var prop in john){
console.log(prop +"::"+john[prop]);
}

console.log("----------------------------------");

Employee.prototype.getFullName=function(){
return this.fname+" "+this.lname;
}

for(var prop in john){
console.log(prop +"::"+john[prop]);
}

forIn2

This means that the for-in loop not only grab the properties on john object but also on its prototype, if enumerable.

What if I want to know the properties on john object only.


function Employee(fname,lname){
this.fname=fname;
this.lname=lname;
}

var john=new Employee("John","Doe");

Employee.prototype.getFullName=function(){
return this.fname+" "+this.lname;
}

for(var prop in john){
if(john.hasOwnProperty(prop)){
console.log(prop +"::"+john[prop]);
}
}

forIn1

For arrays its better to use normal for than for-in as it may lead to wrong result sometime, if someone or some library adds enumerable properties to the prototype.

Javascript is always pass by value

Javascript is always pass by value, but when a variable refers to an object (including arrays), the “value” is a reference to the object. Changing the value of a variable never changes the underlying primitive or object, it just points the variable to a new primitive or object. However, changing a property of an object referenced by a variable does change the underlying object.


function changeObject(x) {
x = {member:"bar"};
console.log("in changeObject: " + x.member);
}

function changeMember(x) {
x.member = "bar";
console.log("in changeMember: " + x.member);
}

var x = {member:"foo"};

console.log("before changeObject: " + x.member);
changeObject(x);
console.log("after changeObject: " + x.member); /* change did not persist */
console.log("-----------------------------------------");
console.log("before changeMember: " + x.member);
changeMember(x);
console.log("after changeMember: " + x.member); /* change persists */

passByValue

Some do argue that this behavior is called call-by-sharing.

Advertisements
%d bloggers like this: