Object-Oriented-JavaScript

Is a way of logically organizing our code in a way that makes sense with respect to the problem we are trying to solve

Every Single ‘object’ is built by a constructor function. Each time a constructor is called a brand new object is created. A constructor make an object linked to it’s own prototype.

Constructor- A constructor is simply a function that uses new to create an object. Any function can be a constructor . By convention constructor in JS begins with a capital letter to distinguish them from non constructor functions.

var object = new Object();

Objects are reference type and reference types do not store the object directly into the variable to which it is assigned, so the object variable in this example doesn’t actually contain the object instance. Instead it holds a pointer(or reference) to the location in memory where the object exists.

When you are assigning an object to a variable you are actually assigning a pointer. That means if you assign one variable to another, each variable gets a copy of the pointer and both still refer the same object in memory.

Best way to identify an array. We can use instanceof as well but it doesn’t work in the case when we are passing an array from one frame to another.

var arr = [ ] ;
Array.isArray()

With primitive wrapper type , properties seem to disappear because the object on which the property was assigned is destroyed immediately afterwards.

Functions

What distinguishes function from any other object is the presence of internal property named [[call]]. This property is unique to functions and indicates that object can be executed.

function overloading is the ability of a single function to have multiple signatures. Thus a single function can have one signature that accepts a single string argument and another that accepts two numeric argument. The language determined which version to call based on the arguments passed in. But JS can accept any number of parameters and type aren’t specified at all. So JS doesn’t have signatures which means lack of function overloading.

In JavaScript, however, when you define multiple functions with the same name, the one that appears last in your code wins. The earlier function declarations are completely removed, and the last is the one that is used.

But we can mimic function overloading in JS.

The Call() Method

Function method for manipulating this which executes the function with a particular this value and with specific parameters. First parameter is the value this should be equal and all subsequent parameters are parameters that should be passed into the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function sayNameForAll(label) {
console.log(label + ":" + this.name);
}
var person1 = {
name: "Nicholas"
};
var person2 = {
name: "Greg"
};
var name = "Michael";
sayNameForAll.call(this, "global"); // outputs "global:Michael"
sayNameForAll.call(person1, "person1"); // outputs "person1:Nicholas"
sayNameForAll.call(person2, "person2"); // outputs "person2:Greg"

The apply() Method

Second method for manipulating this . Works exactly the same as call except that it only accepts two parameters: the value for this and an array or an array like object for parameters(that means we can use arguments object as well)

1
2
3
4
5
6
7
8
9
10
11
12
13
function sayNameForAll(label) {
console.log(label + ":" + this.name);
}
var person1 = {
name: "Nicholas"
}
var person2 = {
name: "Greg"
};
var name = "Michael";
sayNameForAll.call(this, ["global"]); // outputs "global:Michael"
sayNameForAll.call(person1, ["person1"]); // outputs "person1:Nicholas"
sayNameForAll.call(person2, ["person2"]); // outputs "person2:Greg"

Call and bind doesn’t create copy of the function they executes them right away.

The bind() Method

Third method for changing this is bind. It behaves quite differently than other two. The first argument of bind is the this value for the new function.All other arguments represent named parameters that should be permanently set in the new function. We can still pass in any parameters that aren’t permanently set later.

Dereferencing Object

JavaScript is a garbage-collected language, so you don’t really need to worry about memory allocations when you use reference types. However, it’s best to dereference objects that you no longer need so that the garbage collector can free up that memory. The best way to do this is to set the object variable to null.

var object = new Object();

// do something

object = null

Here Object is created used and finally being set to null.

Prototype

A function’s prototype is the object instance that will become the prototype for all objects created using this function as a constructor.

An object’s prototype is the object instance from which the object is inherited.

Proudly powered by Hexo and Theme by Hacker
© 2017 Mohit Garg