Extending javascript part one

94
Extending JavaScript part one

description

Creating custom collections in javascript.

Transcript of Extending javascript part one

Page 1: Extending javascript part one

Extending JavaScriptpart one

Page 2: Extending javascript part one

Everything should be as simple as it is, but not simpler.

Albert Einstein

Page 3: Extending javascript part one

Javascript is a loosely-typedlanguage

Page 4: Extending javascript part one

Javascript is a loosely-typed language

var x = 23; //number

Page 5: Extending javascript part one

Javascript is a loosely-typed language

var x = 23; //number

x = "Apple"; //string

Page 6: Extending javascript part one

Javascript is a loosely-typed language

var x = 23; //number

x = "Apple"; //string

x = new Array(); //object

Page 7: Extending javascript part one

Javascript is a loosely-typed language

var x = 23; //number

x = "Apple"; //string

x = new Array(); //object

Any thing can be assigned to x.

Page 8: Extending javascript part one

What we are going to seein this presentation?

Page 9: Extending javascript part one

What we are going to see in this presentation?

Creating type-safe collections like stack and queue.

Page 10: Extending javascript part one

What we are going to see in this presentation?

Creating type-safe collections like stack and queue.

Page 11: Extending javascript part one

Creating type-safe collections

Page 12: Extending javascript part one

Creating type-safe collections

For help..

Add two methods into the Array class; one to know whether an array contains particular item and the other to remove an item at particular index.

Page 13: Extending javascript part one

Creating type-safe collections

For help..

Add two methods into the Array class; one to know whether an array contains particular item and the other to remove an item at particular index.

Array.prototype.contains = function(element) {return this.indexOf(element) > -1;

}

Array.prototype.remove = function(index) {return this.splice(index, 1);

};

Page 14: Extending javascript part one

Creating type-safe collections

For help..

Create an array that contains the primitive types.

var primitives = ["number","string","object","boolean","function"];

Page 15: Extending javascript part one

Let's make our hands dirty

Page 16: Extending javascript part one

Stack

Page 17: Extending javascript part one

Stack

Plan

Create a new class named Stack.

Page 18: Extending javascript part one

Stack

Plan

Create a new class named Stack.

Encapsulate an instance of array in a property.

Page 19: Extending javascript part one

Stack

Plan

Create a new class named Stack.

Encapsulate an instance of array in a property.

Create a constructor that takes a parameter for the type of data to be stored.

Page 20: Extending javascript part one

Stack

Plan

Create a new class named Stack.

Encapsulate an instance of array in a property.

Create a constructor that takes a parameter for the type of data to be stored.

For storing primitive types pass the type name to the constructor.

var myStack = new Stack("string");

Page 21: Extending javascript part one

Stack

Plan

For storing custom types pass the type itself to the constructor.

var Employee = function() { }; //custom type

var myStack = new Stack(Employee);

Page 22: Extending javascript part one

Stack

Plan

For storing custom types pass the type itself to the constructor.

var Employee = function() { }; //custom type

var myStack = new Stack(Employee);

Check the type is valid in the constructor.

Page 23: Extending javascript part one

Stack

Plan

For storing custom types pass the type itself to the constructor.

var Employee = function() { }; //custom type

var myStack = new Stack(Employee);

Check the type is valid in the constructor.

Create a method named push() to push items into the collection.

Page 24: Extending javascript part one

Stack

Plan

For storing custom types pass the type itself to the constructor.

var Employee = function() { }; //custom type

var myStack = new Stack(Employee);

Check the type is valid in the constructor.

Create a method named push() to push items into the collection.

Create a method named pop() to remove the last added item from the collection.

Page 25: Extending javascript part one

Stack

Plan

Create a method named getValue() to get the item at particular index.

Page 26: Extending javascript part one

Stack

Plan

Create a method named getValue() to get the item at particular index.

Create a method named setValue() to set the item at particular index.

Page 27: Extending javascript part one

Stack

Plan

Create a method named getValue() to get the item at particular index.

Create a method named setValue() to set the item at particular index.

Create a property named length that returns the total no. of items in the collection.

Page 28: Extending javascript part one

Stack

Action

Create a new class named Stack.

var Stack = function(type){}

Page 29: Extending javascript part one

Stack

Action

Create a new class named Stack.

var Stack = function(type){}

Function is a first class object in javascript.

Page 30: Extending javascript part one

Stack

Action

Create a new class named Stack.

var Stack = function(type){}

Function is a first class object in javascript.

Stack is the class name.

Page 31: Extending javascript part one

Stack

Action

Create a new class named Stack.

var Stack = function(type){}

Function is a first class object in javascript.

Stack is the class name.

type - data type to be stored.

Page 32: Extending javascript part one

Stack

Action

Create a new class named Stack.

var Stack = function(type){}

Function is a first class object in javascript.

Stack is the class name.

type - data type to be stored.

{} - the body code of the function is the constructor.

Page 33: Extending javascript part one

Stack

Action

The constructor should take a parameter for the type of data to be stored.

var Stack = function(type){

if(arguments.length != 1)throw new Error("There is no constructor that takes " + arguments.length + " arguments");

...

Page 34: Extending javascript part one

Stack

Action

The constructor should take a parameter for the type of data to be stored.

var Stack = function(type){

if(arguments.length != 1)throw new Error("There is no constructor that takes " + arguments.length + " arguments");

...

If the constructor takes less or more than one parameter throw error.

Page 35: Extending javascript part one

Stack

Action

Check the type is valid in the constructor.

var Stack = function(type){

if(arguments.length != 1)throw new Error("There is no constructor that takes " + arguments.length + " arguments");

if(primitives.contains(type))this.isPrimitive = true;

else if(typeof type == "function")this.isPrimitive = false;

elsethrow new Error("Invalid Type");

...

Page 36: Extending javascript part one

Stack

Action

Check the type is valid in the constructor.

var Stack = function(type){

if(arguments.length != 1)throw new Error("There is no constructor that takes " + arguments.length + " arguments");

if(primitives.contains(type))this.isPrimitive = true;

else if(typeof type == "function")this.isPrimitive = false;

elsethrow new Error("Invalid Type");

...If the type is primitive set isPrimitiveproperty to true else false.

Page 37: Extending javascript part one

Stack

Action

Encapsulate an instance of array in a property.

this.type = type;this.array = new Array();this.length = this.array.length;return this;

} //constructor ends

store the type in a property if it's valid.

Page 38: Extending javascript part one

Stack

Action

Encapsulate an instance of array in a property.

this.type = type;this.array = new Array();this.length = this.array.length;return this;

} //constructor ends

store an instance of array in a property.

Page 39: Extending javascript part one

Stack

Action

Create a property named length that returns the total no. of items in the collection.

this.type = type;this.array = new Array();this.length = this.array.length;return this;

} //constructor ends

store the length of array in a property.

Page 40: Extending javascript part one

Stack

Action

Set the constructor for the Stack.

Stack.prototype.constructor = Stack;

Page 41: Extending javascript part one

Stack

Action

Set the constructor for the Stack.

Stack.prototype.constructor = Stack;

every function has a prototype property that helps in inheriting, overriding and adding new methods to the class on fly.

Page 42: Extending javascript part one

Stack

Action

Create a method named push() to push items into the collection.

Stack.prototype.push = function(){}

Page 43: Extending javascript part one

Stack

Action

Override the push() method to check if the data is of the specified type.

Stack.prototype.push = function(){}

push - accepts multiple items atonce.

myArray.push(12, 23, 34);

So iterate through the arguments list and check each data is valid.

Page 44: Extending javascript part one

Stack

Action

Create a method named push() to push items into the collection.

...var is Valid;

for(var i = 0, j = arguments.length; i < j;i++){

isValid = this.isPrimitive ? (this.type.toLowerCase() == typeof arguments[i])

: (arguments[i] instanceof this.type);

if(!isValid)throw new Error("Invalid Argument");

} //loop ends

Page 45: Extending javascript part one

Stack

Action

Create a method named push() to push items into the collection.

...var is Valid;

for(var i = 0, j = arguments.length; i < j;i++){

isValid = this.isPrimitive ? (this.type.toLowerCase() == typeof arguments[i])

: (arguments[i] instanceof this.type);

if(!isValid)throw new Error("Invalid Argument");

} //loop ends

If any item is not valid throw error.

Page 46: Extending javascript part one

Stack

Action

Create a method named push() to push items into the collection.

for(var i = 0, j = arguments.length; i < j;i++){this.array.push(arguments[i]);

}this.length = this.array.length;return this.array.length;

} //push() ends

Page 47: Extending javascript part one

Stack

Action

Create a method named push() to push items into the collection.

for(var i = 0, j = arguments.length; i < j;i++){this.array.push(arguments[i]);

}this.length = this.array.length;return this.array.length;

} //push() ends

push all the items to the internal array.

Page 48: Extending javascript part one

Stack

Action

Create a method named push() to push items into the collection.

for(var i = 0, j = arguments.length; i < j;i++){this.array.push(arguments[i]);

}this.length = this.array.length;return this.array.length;

} //push() ends

set the length property.

Page 49: Extending javascript part one

Stack

Action

Create a method named pop() to remove the last added item from the collection.

Stack.prototype.pop = function(){this.array.pop();this.length = this.array.length;return this.array.length;

}

Page 50: Extending javascript part one

Stack

Action

Create a method named getValue() to get the item at particular index.

Stack.prototype.getValue = function(index){ return this.array[index];

}

Page 51: Extending javascript part one

Stack

Action

Create a method named setValue() to set the item at particular index.

Stack.prototype.getValue = function(index){ return this.array[index];

}

Stack.prototype.setValue = function(index, value){var isValid = this.isPrimitive ? (this.type.toLowerCase() == typeof value) : (value instanceof this.type);

if(!isValid)throw new Error("Invalid Argument");

this.array[index] = value;return this.array[index];

}

Page 52: Extending javascript part one

Stack

Complete

var Stack = function(type){

if(arguments.length != 1)throw new Error("There is no constructor that takes " + arguments.length + " arguments");

if(primitives.contains(type))this.isPrimitive = true;

else if(typeof type == "function")this.isPrimitive = false;

elsethrow new Error("Invalid Type");

this.type = type;this.array = new Array();this.length = this.array.length;return this;

}

Page 53: Extending javascript part one

Stack

Complete

Stack.prototype.constructor = Stack;

Stack.prototype.push = function(){

var is Valid;

for(var i = 0, j = arguments.length; i < j;i++){

isValid = this.isPrimitive ? (this.type.toLowerCase() == typeof arguments[i])

: (arguments[i] instanceof this.type);

if(!isValid)throw new Error("Invalid Argument");

}

Page 54: Extending javascript part one

Stack

Complete

for(var i = 0, j = arguments.length; i < j;i++){this.array.push(arguments[i]);

}this.length = this.array.length;return this.array.length;

}

Stack.prototype.pop = function(){ this.array.pop();this.length = this.array.length;return this.array.length;

}

Stack.prototype.getValue = function(index){ return this.array[index];

}

Page 55: Extending javascript part one

Stack

Complete

Stack.prototype.setValue = function(index, value){var isValid = this.isPrimitive ? (this.type.toLowerCase() == typeof value) : (value instanceof this.type);

if(!isValid)throw new Error("Invalid Argument");

this.array[index] = value;return this.array[index];

}

Page 56: Extending javascript part one

It's time to test

Page 57: Extending javascript part one

It's time to test

Steps

Start the firefox.

Page 58: Extending javascript part one

It's time to test

Steps

Start the firefox.

Open the firebug console window.

Page 59: Extending javascript part one

It's time to test

Steps

Testing constructor.

var myStack = new Stack();

Page 60: Extending javascript part one

It's time to test

Steps

Testing constructor.

Error: There is no constructor that takes 0 arguments

var myStack = new Stack();

Page 61: Extending javascript part one

It's time to test

Steps

Testing constructor.

var myStack = new Stack("string1");

Page 62: Extending javascript part one

It's time to test

Steps

Testing constructor.

Error: Invalid Type

var myStack = new Stack("string1");

Page 63: Extending javascript part one

It's time to test

Steps

Testing push().

var myStack = new Stack("string");

myStack.push(1);

Page 64: Extending javascript part one

It's time to test

Steps

Testing push().

Error: Invalid Argument

var myStack = new Stack("string");

myStack.push(1);

Page 65: Extending javascript part one

It's time to test

Steps

Testing push().

var myStack = new Stack("string");

myStack.push("Apple");

Page 66: Extending javascript part one

It's time to test

Steps

Testing push().

var myStack = new Stack("string");

myStack.push("Apple");

1

Page 67: Extending javascript part one

It's time to test

Steps

Testing push().

var myStack = new Stack("string");

myStack.push("Apple");

myStach.push("Orange", 3);

Page 68: Extending javascript part one

It's time to test

Steps

Testing push().

var myStack = new Stack("string");

myStack.push("Apple");

myStach.push("Orange", 3);

Error: Invalid Argument

Page 69: Extending javascript part one

It's time to test

Steps

Testing push().

var Employee = function(name) { this.name = name };

Page 70: Extending javascript part one

It's time to test

Steps

Testing push().

var Employee = function(name) { this.name = name };

var myStack = new Stack(Employee);

Page 71: Extending javascript part one

It's time to test

Steps

Testing push().

var Employee = function(name) { this.name = name };

var myStack = new Stack(Employee);

myStack.push("Apple");

Page 72: Extending javascript part one

It's time to test

Steps

Testing push().

var Employee = function(name) { this.name = name };

var myStack = new Stack(Employee);

myStack.push("Apple");

Error: Invalid Argument

Page 73: Extending javascript part one

It's time to test

Steps

Testing push().

var Employee = function(name) { this.name = name };

var myStack = new Stack(Employee);

myStack.push(new Employee("Stephenson"));

1

Page 74: Extending javascript part one

It's time to test

Steps

Testing getValue().

myStack.getValue(0);

Page 75: Extending javascript part one

It's time to test

Steps

Testing getValue().

myStack.getValue(0);

Object { name="Stephenson" }

Page 76: Extending javascript part one

Queue

Page 77: Extending javascript part one

Queue

Plan

Most of the stuff are same like Stack except some things.

Page 78: Extending javascript part one

Queue

Plan

Most of the stuff are same like Stack except some things.

Change the names of push() and pop() as enter() and exit().

Page 79: Extending javascript part one

Queue

Plan

Most of the stuff are same like Stack except some things.

Change the names of push() and pop() as enter() and exit().

In Stack the last added item comes out first while in Queue it's opposite. Change the exit() implementation.

Page 80: Extending javascript part one

Queue

Action

The implementation of all the methods except exit() is same as Stack. Let's ignore them in discussion.

Page 81: Extending javascript part one

Queue

Action

Change the exit() implementation.

Queue.prototype.exit = function(){this.array.remove(0);this.length = this.array.length;return this.length;

}

Page 82: Extending javascript part one

Queue

Action

Change the exit() implementation.

Queue.prototype.exit = function(){this.array.remove(0);this.length = this.array.length;return this.length;

}

remove the first item from the internal array by calling the remove() method added to the Array class.

Page 83: Extending javascript part one

Queue

Complete

var Queue = function(type){

if(arguments.length != 1)throw new Error("There is no constructor that takes " + arguments.length + " arguments");

if(primitives.contains(type))this.isPrimitive = true;

else if(typeof type == "function")this.isPrimitive = false;

elsethrow new Error("Invalid Type");

this.type = type;this.array = new Array();this.length = this.array.length;return this;

}

Page 84: Extending javascript part one

Queue

Complete

Queue.prototype.constructor = Queue;

Queue.prototype.enter = function(){

var is Valid;

for(var i = 0, j = arguments.length; i < j;i++){

isValid = this.isPrimitive ? (this.type.toLowerCase() == typeof arguments[i])

: (arguments[i] instanceof this.type);

if(!isValid)throw new Error("Invalid Argument");

}

Page 85: Extending javascript part one

Queue

Complete

for(var i = 0, j = arguments.length; i < j;i++){this.array.push(arguments[i]);

}this.length = this.array.length;return this.array.length;

}

Queue.prototype.exit = function(){ this.array.remove(0);this.length = this.array.length;return this.length;

}

Queue.prototype.getValue = function(index){ return this.array[index];

}

Page 86: Extending javascript part one

Queue

Complete

Queue.prototype.setValue = function(index, value){var isValid = this.isPrimitive ? (this.type.toLowerCase() == typeof value) : (value instanceof this.type);

if(!isValid)throw new Error("Invalid Argument");

this.array[index] = value;return this.array[index];

}

Page 87: Extending javascript part one

It's time to test

Page 88: Extending javascript part one

It's time to test

Steps

Testing exit().

var myQueue = new Queue("string");

Page 89: Extending javascript part one

It's time to test

Steps

Testing exit().

var myQueue = new Queue("string");

myQueue.enter("RED", "BLUE", "GREEN");

Page 90: Extending javascript part one

It's time to test

Steps

Testing exit().

var myQueue = new Queue("string");

myQueue.enter("RED", "BLUE", "GREEN");

myQueue.exit();

Page 91: Extending javascript part one

It's time to test

Steps

Testing exit().

var myQueue = new Queue("string");

myQueue.enter("RED", "BLUE", "GREEN");

myQueue.exit();

Page 92: Extending javascript part one

It's time to test

Steps

Testing exit().

myQueue.getValue(0);

Page 93: Extending javascript part one

It's time to test

Steps

Testing exit().

myQueue.getValue(0);

"BLUE"

Page 94: Extending javascript part one

Thank You