KEMBAR78
Extending javascript part one | PPTX
Extending JavaScriptpart one
Everythingshould beas simple as it is, but not simpler.  Albert Einstein
Javascript is a loosely-typedlanguage 
Javascript is a loosely-typed languagevar x = 23; //number
Javascript is a loosely-typed languagevar x = 23; //numberx = "Apple"; //string
Javascript is a loosely-typed languagevar x = 23; //numberx = "Apple"; //stringx = new Array(); //object
Javascript is a loosely-typed languagevar x = 23; //numberx = "Apple"; //stringx = new Array(); //objectAny thing can be assigned to x.
What we are going to seein this presentation? 
What we are going to see in this presentation?Creating type-safe collections like stackandqueue.
What we are going to see in this presentation?Creating type-safe collections like stackandqueue.
Creating type-safe collections 
Creating type-safe collectionsFor 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.
Creating type-safe collectionsFor 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);};
Creating type-safe collectionsFor help..Create an array that contains the primitive types.var primitives = ["number", "string","object","boolean","function"];
Let's make our hands dirty 
Stack 
StackPlanCreate a new class named Stack.
StackPlanCreate a new class named Stack.Encapsulate an instance of array in a property.
StackPlanCreate 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.
StackPlanCreate 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");
StackPlanFor storing custom types pass the type itself to the constructor.     var Employee = function() { }; //custom type     var myStack = new Stack(Employee);
StackPlanFor 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.
StackPlanFor 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.
StackPlanFor 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.
StackPlanCreate a method named getValue() to get the item at particular index.
StackPlanCreate a method named getValue() to get the item at particular index.Create a method named setValue() to set the item at particular index.
StackPlanCreate 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.
StackActionCreate a new class named Stack.var Stack = function(type){}
StackActionCreate a new class named Stack.var Stack = function(type){}Function is a first class object in javascript.
StackActionCreate a new class named Stack.var Stack = function(type){}Function is a first class object in javascript.Stack is the class name.
StackActionCreate 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.
StackActionCreate 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.
StackActionThe 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 noconstructor that takes " + arguments.length + " arguments");...
StackActionThe 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 noconstructor that takes " + arguments.length + " arguments");...If the constructor takes less or more than one parameter throw error.
StackActionCheck the type is valid in the constructor.var Stack = function(type){if(arguments.length != 1)throw new Error("There is noconstructor 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");...
StackActionCheck the type is valid in the constructor.var Stack = function(type){if(arguments.length != 1)throw new Error("There is noconstructor 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 isPrimitive property to true else false.
StackActionEncapsulate an instance of array in a property.this.type = type;this.array = new Array();	this.length = this.array.length;return this;} //constructor endsstore the type in a property if it's valid.
StackActionEncapsulate an instance of array in a property.this.type = type;this.array = new Array();	this.length = this.array.length;return this;} //constructor endsstore an instance of array in a property.
StackActionCreate 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 endsstore the length of array in a property.
StackActionSet the constructor for the Stack.Stack.prototype.constructor = Stack;
StackActionSet 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.
StackActionCreate a method named push() to push items into the collection.Stack.prototype.push = function(){}
StackActionOverride 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.
StackActionCreate 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
StackActionCreate 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 endsIf any item is not valid throw error.
StackActionCreate 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
StackActionCreate 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() endspush all the items to the internalarray.
StackActionCreate 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() endsset the length property.
StackActionCreate 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;}
StackActionCreate a method named getValue() to get the item at particular index.Stack.prototype.getValue = function(index){ 	return this.array[index];}
StackActionCreate 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];}
StackCompletevar Stack = function(type){if(arguments.length != 1)throw new Error("There is noconstructor 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;}
StackCompleteStack.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() == typeofarguments[i]): (arguments[i] instanceof this.type);if(!isValid)throw new Error("Invalid Argument");}
StackComplete	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];}
StackCompleteStack.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];}
It's time to test
It's time to testStepsStart the firefox.
It's time to testStepsStart the firefox.Open the firebug console window.
It's time to testStepsTesting constructor.var myStack = new Stack();
It's time to testStepsTesting constructor.var myStack = new Stack();Error: There is no constructor that takes 0 arguments
It's time to testStepsTesting constructor.var myStack = new Stack("string1");
It's time to testStepsTesting constructor.var myStack = new Stack("string1");Error: Invalid Type
It's time to testStepsTesting push().var myStack = new Stack("string");myStack.push(1);
It's time to testStepsTesting push().var myStack = new Stack("string");myStack.push(1);Error: Invalid Argument
It's time to testStepsTesting push().var myStack = new Stack("string");myStack.push("Apple");
It's time to testStepsTesting push().var myStack = new Stack("string");myStack.push("Apple");1
It's time to testStepsTesting push().var myStack = new Stack("string");myStack.push("Apple");myStach.push("Orange", 3);
It's time to testStepsTesting push().var myStack = new Stack("string");myStack.push("Apple");myStach.push("Orange", 3);Error: Invalid Argument
It's time to testStepsTesting push().var Employee = function(name) { this.name = name };
It's time to testStepsTesting push().var Employee = function(name) { this.name = name };var myStack = new Stack(Employee);
It's time to testStepsTesting push().var Employee = function(name) { this.name = name };var myStack = new Stack(Employee);myStack.push("Apple");
It's time to testStepsTesting push().var Employee = function(name) { this.name = name };var myStack = new Stack(Employee);myStack.push("Apple");Error: Invalid Argument
It's time to testStepsTesting push().var Employee = function(name) { this.name = name };var myStack = new Stack(Employee);myStack.push(new Employee("Stephenson"));1
It's time to testStepsTesting getValue().myStack.getValue(0);
It's time to testStepsTesting getValue().myStack.getValue(0);Object { name="Stephenson" }
Queue 
QueuePlanMost of the stuff are same like Stack except some things.
QueuePlanMost of the stuff are same like Stack except some things.Change the names of push() and pop() as enter() and exit().
QueuePlanMost 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. Changethe exit() implementation.
QueueActionThe implementation of all the methods except exit() is same as Stack. Let's ignore them in discussion.
QueueActionChange the exit() implementation.Queue.prototype.exit = function(){this.array.remove(0);	this.length = this.array.length;	return this.length;}
QueueActionChange 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.
QueueCompletevar Queue = function(type){if(arguments.length != 1)throw new Error("There is noconstructor 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;}
QueueCompleteQueue.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() == typeofarguments[i]): (arguments[i] instanceof this.type);if(!isValid)throw new Error("Invalid Argument");}
QueueComplete	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];}
QueueCompleteQueue.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];}
It's time to test
It's time to testStepsTestingexit().var myQueue = new Queue("string");
It's time to testStepsTestingexit().var myQueue = new Queue("string");myQueue.enter("RED", "BLUE", "GREEN");
It's time to testStepsTestingexit().var myQueue = new Queue("string");myQueue.enter("RED", "BLUE", "GREEN");myQueue.exit();
It's time to testStepsTestingexit().var myQueue = new Queue("string");myQueue.enter("RED", "BLUE", "GREEN");myQueue.exit();
It's time to testStepsTestingexit().myQueue.getValue(0);
It's time to testStepsTestingexit().myQueue.getValue(0);"BLUE"
Thank You 

Extending javascript part one

  • 1.
  • 2.
    Everythingshould beas simpleas it is, but not simpler.  Albert Einstein
  • 3.
    Javascript is aloosely-typedlanguage 
  • 4.
    Javascript is aloosely-typed languagevar x = 23; //number
  • 5.
    Javascript is aloosely-typed languagevar x = 23; //numberx = "Apple"; //string
  • 6.
    Javascript is aloosely-typed languagevar x = 23; //numberx = "Apple"; //stringx = new Array(); //object
  • 7.
    Javascript is aloosely-typed languagevar x = 23; //numberx = "Apple"; //stringx = new Array(); //objectAny thing can be assigned to x.
  • 8.
    What we aregoing to seein this presentation? 
  • 9.
    What we aregoing to see in this presentation?Creating type-safe collections like stackandqueue.
  • 10.
    What we aregoing to see in this presentation?Creating type-safe collections like stackandqueue.
  • 11.
  • 12.
    Creating type-safe collectionsForhelp..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.
  • 13.
    Creating type-safe collectionsForhelp..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);};
  • 14.
    Creating type-safe collectionsForhelp..Create an array that contains the primitive types.var primitives = ["number", "string","object","boolean","function"];
  • 15.
    Let's make ourhands dirty 
  • 16.
  • 17.
    StackPlanCreate a newclass named Stack.
  • 18.
    StackPlanCreate a newclass named Stack.Encapsulate an instance of array in a property.
  • 19.
    StackPlanCreate a newclass 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.
  • 20.
    StackPlanCreate a newclass 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");
  • 21.
    StackPlanFor storing customtypes pass the type itself to the constructor. var Employee = function() { }; //custom type var myStack = new Stack(Employee);
  • 22.
    StackPlanFor storing customtypes 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.
  • 23.
    StackPlanFor storing customtypes 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.
  • 24.
    StackPlanFor storing customtypes 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.
  • 25.
    StackPlanCreate a methodnamed getValue() to get the item at particular index.
  • 26.
    StackPlanCreate a methodnamed getValue() to get the item at particular index.Create a method named setValue() to set the item at particular index.
  • 27.
    StackPlanCreate a methodnamed 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.
  • 28.
    StackActionCreate a newclass named Stack.var Stack = function(type){}
  • 29.
    StackActionCreate a newclass named Stack.var Stack = function(type){}Function is a first class object in javascript.
  • 30.
    StackActionCreate a newclass named Stack.var Stack = function(type){}Function is a first class object in javascript.Stack is the class name.
  • 31.
    StackActionCreate a newclass 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.
  • 32.
    StackActionCreate a newclass 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.
  • 33.
    StackActionThe constructor shouldtake a parameter for the type of data to be stored.var Stack = function(type){if(arguments.length != 1)throw new Error("There is noconstructor that takes " + arguments.length + " arguments");...
  • 34.
    StackActionThe constructor shouldtake a parameter for the type of data to be stored.var Stack = function(type){if(arguments.length != 1)throw new Error("There is noconstructor that takes " + arguments.length + " arguments");...If the constructor takes less or more than one parameter throw error.
  • 35.
    StackActionCheck the typeis valid in the constructor.var Stack = function(type){if(arguments.length != 1)throw new Error("There is noconstructor 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");...
  • 36.
    StackActionCheck the typeis valid in the constructor.var Stack = function(type){if(arguments.length != 1)throw new Error("There is noconstructor 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 isPrimitive property to true else false.
  • 37.
    StackActionEncapsulate an instanceof array in a property.this.type = type;this.array = new Array(); this.length = this.array.length;return this;} //constructor endsstore the type in a property if it's valid.
  • 38.
    StackActionEncapsulate an instanceof array in a property.this.type = type;this.array = new Array(); this.length = this.array.length;return this;} //constructor endsstore an instance of array in a property.
  • 39.
    StackActionCreate a propertynamed 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 endsstore the length of array in a property.
  • 40.
    StackActionSet the constructorfor the Stack.Stack.prototype.constructor = Stack;
  • 41.
    StackActionSet the constructorfor 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.
  • 42.
    StackActionCreate a methodnamed push() to push items into the collection.Stack.prototype.push = function(){}
  • 43.
    StackActionOverride 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.
  • 44.
    StackActionCreate a methodnamed 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
  • 45.
    StackActionCreate a methodnamed 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 endsIf any item is not valid throw error.
  • 46.
    StackActionCreate a methodnamed 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
  • 47.
    StackActionCreate a methodnamed 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() endspush all the items to the internalarray.
  • 48.
    StackActionCreate a methodnamed 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() endsset the length property.
  • 49.
    StackActionCreate a methodnamed 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;}
  • 50.
    StackActionCreate a methodnamed getValue() to get the item at particular index.Stack.prototype.getValue = function(index){ return this.array[index];}
  • 51.
    StackActionCreate a methodnamed 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];}
  • 52.
    StackCompletevar Stack =function(type){if(arguments.length != 1)throw new Error("There is noconstructor 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;}
  • 53.
    StackCompleteStack.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() == typeofarguments[i]): (arguments[i] instanceof this.type);if(!isValid)throw new Error("Invalid Argument");}
  • 54.
    StackComplete 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];}
  • 55.
    StackCompleteStack.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];}
  • 56.
  • 57.
    It's time totestStepsStart the firefox.
  • 58.
    It's time totestStepsStart the firefox.Open the firebug console window.
  • 59.
    It's time totestStepsTesting constructor.var myStack = new Stack();
  • 60.
    It's time totestStepsTesting constructor.var myStack = new Stack();Error: There is no constructor that takes 0 arguments
  • 61.
    It's time totestStepsTesting constructor.var myStack = new Stack("string1");
  • 62.
    It's time totestStepsTesting constructor.var myStack = new Stack("string1");Error: Invalid Type
  • 63.
    It's time totestStepsTesting push().var myStack = new Stack("string");myStack.push(1);
  • 64.
    It's time totestStepsTesting push().var myStack = new Stack("string");myStack.push(1);Error: Invalid Argument
  • 65.
    It's time totestStepsTesting push().var myStack = new Stack("string");myStack.push("Apple");
  • 66.
    It's time totestStepsTesting push().var myStack = new Stack("string");myStack.push("Apple");1
  • 67.
    It's time totestStepsTesting push().var myStack = new Stack("string");myStack.push("Apple");myStach.push("Orange", 3);
  • 68.
    It's time totestStepsTesting push().var myStack = new Stack("string");myStack.push("Apple");myStach.push("Orange", 3);Error: Invalid Argument
  • 69.
    It's time totestStepsTesting push().var Employee = function(name) { this.name = name };
  • 70.
    It's time totestStepsTesting push().var Employee = function(name) { this.name = name };var myStack = new Stack(Employee);
  • 71.
    It's time totestStepsTesting push().var Employee = function(name) { this.name = name };var myStack = new Stack(Employee);myStack.push("Apple");
  • 72.
    It's time totestStepsTesting push().var Employee = function(name) { this.name = name };var myStack = new Stack(Employee);myStack.push("Apple");Error: Invalid Argument
  • 73.
    It's time totestStepsTesting push().var Employee = function(name) { this.name = name };var myStack = new Stack(Employee);myStack.push(new Employee("Stephenson"));1
  • 74.
    It's time totestStepsTesting getValue().myStack.getValue(0);
  • 75.
    It's time totestStepsTesting getValue().myStack.getValue(0);Object { name="Stephenson" }
  • 76.
  • 77.
    QueuePlanMost of thestuff are same like Stack except some things.
  • 78.
    QueuePlanMost of thestuff are same like Stack except some things.Change the names of push() and pop() as enter() and exit().
  • 79.
    QueuePlanMost of thestuff 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. Changethe exit() implementation.
  • 80.
    QueueActionThe implementation ofall the methods except exit() is same as Stack. Let's ignore them in discussion.
  • 81.
    QueueActionChange the exit()implementation.Queue.prototype.exit = function(){this.array.remove(0); this.length = this.array.length; return this.length;}
  • 82.
    QueueActionChange 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.
  • 83.
    QueueCompletevar Queue =function(type){if(arguments.length != 1)throw new Error("There is noconstructor 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;}
  • 84.
    QueueCompleteQueue.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() == typeofarguments[i]): (arguments[i] instanceof this.type);if(!isValid)throw new Error("Invalid Argument");}
  • 85.
    QueueComplete 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];}
  • 86.
    QueueCompleteQueue.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];}
  • 87.
  • 88.
    It's time totestStepsTestingexit().var myQueue = new Queue("string");
  • 89.
    It's time totestStepsTestingexit().var myQueue = new Queue("string");myQueue.enter("RED", "BLUE", "GREEN");
  • 90.
    It's time totestStepsTestingexit().var myQueue = new Queue("string");myQueue.enter("RED", "BLUE", "GREEN");myQueue.exit();
  • 91.
    It's time totestStepsTestingexit().var myQueue = new Queue("string");myQueue.enter("RED", "BLUE", "GREEN");myQueue.exit();
  • 92.
    It's time totestStepsTestingexit().myQueue.getValue(0);
  • 93.
    It's time totestStepsTestingexit().myQueue.getValue(0);"BLUE"
  • 94.