JS, Understanding Weird parts-2 (sec 4, Objects & Functions)

In Javascript objects and functions are very much simillar. ‘.’ (dot) operator is used to access the members of any object. person.age  returns the age of the person object. This is left associative operator.

Object Literal: {} is called object literal, equivalent to new Object();

var person = {
    firstName: "sajib",
    lastName: "biswas"
};

console.log(person.firstName);  // sajib

Namespace is a container for variables and functions.

Javascript objects and JSON data has almost simillar structure except that we have must wrap both key and values in double quote( ” ” ).

var person = {
    firstName: "sajib",
    lastName: "biswas"
};

// JS object to JSON string
var json = JSON.stringify(person);

console.log(json);          // {"firstName":"sajib","lastName":"biswas"}
console.log( typeof json);  // string


// JSON string to object conversion
var obj = JSON.parse(json);

console.log(obj);          // Object {"firstName":"sajib","lastName":"biswas"}
console.log( typeof obj);  // object

First Class Functions: In JS everything we can do with other types, we can do same with functions. Functions are special type of object. The codes inside a function is just one property of that object (invokable code).

We can attach a variable to a function coz in JS functions are object.

function greet(){
    console.log("good night");
}

greet.english = "hello everyone";
console.log(greet.english);         // hello everyone

Expression: A unit of code that results a value. ex: 2 + 3 ;, var a = 7; etc..

Statement: A statement doesnt return a value. ex. if statement, while statement.

Function statements & function Expression:  In the following code block, first example is a function statement as this does not yields to a value. Whereas, the second one yields a value which is assigned to the var foo.

// Function statement
greet();            // hello. It is hoisted

function greet(){  
    console.log("hello");
}

//Function expression
foo();              // Error. foo is not a function

var foo = function greet(){
    console.log("hello");
};

By value or By reference: For primitives, JS copies by value, i.e.: it creates a new variable and also creates a new memory location and puts a new value in the new location. But for object, it creates a new variable but the reference is same (now both old and new variable points to same object). All primitives are copied by value and all objects including functions are copied by reference.

Mutate: To change something.

Immutable: means it cant be changed.

this keyword:

function a(){
	console.log(this);
}

var b = function(){
	console.log(this);
};

a();	// window
b();	// window

var c = {
	name: 'sajib',
	log: function(){
		console.log(this);
	}
};

c.log();	// object {name: "sajib"}

 

// functions which are inside of methods of objects: this refers to window object 

var c = {
	name: 'sajib',
	log: function(){
		console.log(this);
		function innerFoo(){
			console.log(this);
		}
		innerFoo();
	}
};

c.log();	// object {name: "sajib} and window object

start from 40

Leave a Reply

Your email address will not be published. Required fields are marked *