JS: Good parts - Ch.4 - Functions

arguments return

by Denise Nepraunig

JavaScript

/* 
All texts and infos I have taken from:
'JavaScript: The Good Parts' by Douglas Crockford
*/

// ARGUMENTS
// all functions have this 'bonus' parameter arguments
// don't let it fool you, it is array-like, but doesn't 
// have all the array function

var sum = function sum() {
    var i, sum = 0;
    for(i = 0; i < arguments.length; i++) {
        sum += arguments[i];
    }
    return sum;
};

var mySum = sum(1,2,3,4,5);
console.log("sum should be 15:", mySum);

// RETURN
// a function ends when it reaches } or by return
// a function always returns a value
// if none was defined, it will return undefined
// if the function was invoked with new, then the new object is retured

var levoid = function levoid() {
    // i am doing nothing
};

var myVoid = levoid();
console.log("myVoid should be undefined", myVoid);

var Cat = function Cat() {};
var myCat = new Cat();
console.log("myCat should be an empty object", myCat);
console.log("myCat is instance of cat", myCat instanceof Cat);