Bits and Bytes of Javascript : Part 1 (Objects and Functions)

Hi Reader ! This is the first part of the Bits and Bytes of the Javascript stories where I intend to write about the important concepts of the Javascript.

In this 1st part I will be writing about the two very basic concepts namely “Objects” and “Functions” in Javascript.

Javascript treats everything as an object. Arrays are objects, functions are object, Dates ;-) are Objects, Math is Object, Regexs are Objects, and also Objects are objects :D .

Different Ways of defining/declaring an Object :

  1. var myObject = {}; //defines an empty Object named “myObject”
  2. var person = {
    person.name = “Siddharth”,
    person.getName : function(){
    return this.name;
    }
    }
    Both the above mentioned approaches are called “Literal Notation”.
  3. Using Object Constructor :
    var person = new Object();
    person.name = “Siddharth”;
  4. Using Factory function :
    var newPerson = function (name){
    var homosapien= new Object();
     homosapien.name = name;
     homosapien.getName = function(){
     return this.name;
     };
     return homosapien;
    };
    var person1 = newPerson(“Siddharth”);
    var person2 = newPerson(“Chinu”);
    console.log(person1.getName()); // prints Siddharth
    console.log(person2.getName()); // prints Chinu
  5. Using Constructor function :
    function Person(name){
    this.name = name;
    this.getName = function(){
    return this.name;
    };
    };
    var person1 = new Person(“Chinu”);
    console.log(person1.getName()); // prints Chinu
    console.log(person1 instanceOf Person); // will return true
    console.log(person1.constructor === Person); // will return true

There is one more method using “prototype”, about which I shall write later after writing the story on “prototypical inheritance in JS”. For further reference I shall recommend going through this link on w3schools.

What Douglas Crockford has to say about using “new” :


Functions : Why, How and Everything Important !

Lets start by looking at different aspects of defining and calling functions.

Approach 1 -

function abc(){
//do something
}

Approach 2 -

var abc = function(){
//do something
}

Think of the difference between the two approaches !! I promise, I will discuss them soon (in my next story i.e Part 2 of the same series).

Function objects are created with function literals:
// Create a variable called add and store a function
// in it that adds two numbers.
var add = function (a, b) {
return a + b;
};
A function literal has four parts.

  1. First Part- reserved word function.
  2. Second Part (optional)- function’s name.In the example above, I haven’t given any name to function & hence we call it anonymous.
  3. Third Part- Set of parameters of the function, wrapped in parentheses. Within the parentheses is a set of zero or more parameter names, separated by commas. These names will be defined as variables in the function. They will be initialized to the arguments supplied when the function is invoked or called.
  4. Fourth Part- Body of the function, which gets executed when the function is invoked.

Happy Coding ! :)