SlideShare a Scribd company logo
1 of 94
Extending JavaScript part one
Everythingshould beas simple as it is, but not simpler.   Albert Einstein
Javascript is a loosely-typed language 
Javascript is a loosely-typed language var x = 23; //number
Javascript is a loosely-typed language var x = 23; //number x = "Apple"; //string
Javascript is a loosely-typed language var x = 23; //number x = "Apple"; //string x = new Array(); //object
Javascript is a loosely-typed language var x = 23; //number x = "Apple"; //string x = new Array(); //object Any thing can be assigned to x.
What we are going to see in 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 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.
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); };
Creating type-safe collections For help.. Create an array that contains the primitive types. var primitives = ["number",  "string", "object", "boolean", "function"];
Let's make our hands dirty 
Stack 
Stack Plan Create a new class named Stack.
Stack Plan Create a new class named Stack. Encapsulate an instance of array in a property.
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.
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");
Stack Plan For storing custom types pass the type itself to the constructor.      var Employee = function() { }; //custom type      var myStack = new Stack(Employee);
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.
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.
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.
Stack Plan Create a method named getValue() to get the item at particular index.
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.
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.
Stack Action Create a new class named Stack. var Stack = function(type){ }
Stack Action Create a new class named Stack. var Stack = function(type){ } Function is a first class object in javascript.
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.
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.
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.
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 noconstructor that takes " + arguments.length + " arguments"); ...
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 noconstructor that takes " + arguments.length + " arguments"); ... If the constructor takes less or more than one parameter throw error.
Stack Action Check 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; else throw new Error("Invalid Type"); ...
Stack Action Check 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; else throw new Error("Invalid Type"); ... If the type is primitive set isPrimitive property to true else false.
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.
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.
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.
Stack Action Set the constructor for the Stack. Stack.prototype.constructor = Stack;
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.
Stack Action Create a method named push() to push items into the collection. Stack.prototype.push = function(){ }
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.
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
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.
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
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 internalarray.
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.
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; }
Stack Action Create a method named getValue() to get the item at particular index. Stack.prototype.getValue = function(index){  	return this.array[index]; }
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]; }
Stack Complete 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; else throw new Error("Invalid Type"); this.type = type; this.array = new Array(); 	this.length = this.array.length; 	return this; }
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() == typeofarguments[i]) : (arguments[i] instanceof this.type); if(!isValid) throw new Error("Invalid Argument"); }
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]; }
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]; }
It's time to test
It's time to test Steps Start the firefox.
It's time to test Steps Start the firefox. Open the firebug console window.
It's time to test Steps Testing constructor. var myStack = new Stack();
It's time to test Steps Testing constructor. var myStack = new Stack(); Error: There is no constructor that takes 0  arguments
It's time to test Steps Testing constructor. var myStack = new Stack("string1");
It's time to test Steps Testing constructor. var myStack = new Stack("string1"); Error: Invalid Type
It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push(1);
It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push(1); Error: Invalid Argument
It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push("Apple");
It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push("Apple"); 1
It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push("Apple"); myStach.push("Orange", 3);
It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push("Apple"); myStach.push("Orange", 3); Error: Invalid Argument
It's time to test Steps Testing push(). var Employee = function(name) { this.name = name };
It's time to test Steps Testing push(). var Employee = function(name) { this.name = name }; var myStack = new Stack(Employee);
It's time to test Steps Testing push(). var Employee = function(name) { this.name = name }; var myStack = new Stack(Employee); myStack.push("Apple");
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
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
It's time to test Steps Testing getValue(). myStack.getValue(0);
It's time to test Steps Testing getValue(). myStack.getValue(0); Object { name="Stephenson" }
Queue 
Queue Plan Most of the stuff are same like Stack except some things.
Queue Plan Most of the stuff are same like Stack except some things. Change the names of push() and pop() as enter() and exit().
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. Changethe exit() implementation.
Queue Action The implementation of all the methods except exit() is same as Stack. Let's ignore them in discussion.
Queue Action Change the exit() implementation. Queue.prototype.exit = function(){ this.array.remove(0); 	this.length = this.array.length; 	return this.length; }
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.
Queue Complete var 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; else throw new Error("Invalid Type"); this.type = type; this.array = new Array(); 	this.length = this.array.length; 	return this; }
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() == typeofarguments[i]) : (arguments[i] instanceof this.type); if(!isValid) throw new Error("Invalid Argument"); }
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]; }
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]; }
It's time to test
It's time to test Steps Testingexit(). var myQueue = new Queue("string");
It's time to test Steps Testingexit(). var myQueue = new Queue("string"); myQueue.enter("RED", "BLUE", "GREEN");
It's time to test Steps Testingexit(). var myQueue = new Queue("string"); myQueue.enter("RED", "BLUE", "GREEN"); myQueue.exit();
It's time to test Steps Testingexit(). var myQueue = new Queue("string"); myQueue.enter("RED", "BLUE", "GREEN"); myQueue.exit();
It's time to test Steps Testingexit(). myQueue.getValue(0);
It's time to test Steps Testingexit(). myQueue.getValue(0); "BLUE"
Thank You 

More Related Content

What's hot

Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckFranklin Chen
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and ClosuresSandip Kumar
 
Collections Framework
Collections FrameworkCollections Framework
Collections FrameworkSunil OS
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaDerek Chen-Becker
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Sanjeev_Knoldus
 
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Andrew Phillips
 

What's hot (19)

Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheck
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
 
Java script arrays
Java script arraysJava script arrays
Java script arrays
 
Scala
ScalaScala
Scala
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
 
What are arrays in java script
What are arrays in java scriptWhat are arrays in java script
What are arrays in java script
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Scala cheatsheet
Scala cheatsheetScala cheatsheet
Scala cheatsheet
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
 
Scala 2013 review
Scala 2013 reviewScala 2013 review
Scala 2013 review
 
02 stackqueue
02 stackqueue02 stackqueue
02 stackqueue
 
Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
 
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
 
Scala coated JVM
Scala coated JVMScala coated JVM
Scala coated JVM
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 

Similar to Extending javascript part one

Stack Implementation
Stack ImplementationStack Implementation
Stack ImplementationZidny Nafan
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manualChandrapriya Jayabal
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesDragos Ionita
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
power-assert, mechanism and philosophy
power-assert, mechanism and philosophypower-assert, mechanism and philosophy
power-assert, mechanism and philosophyTakuto Wada
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfinfo309708
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureSriram Raj
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Conceptsmdfkhan625
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalMichael Stal
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEDarwin Durand
 

Similar to Extending javascript part one (20)

Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best Practices
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
Lec2
Lec2Lec2
Lec2
 
Java Generics
Java GenericsJava Generics
Java Generics
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
power-assert, mechanism and philosophy
power-assert, mechanism and philosophypower-assert, mechanism and philosophy
power-assert, mechanism and philosophy
 
OO in JavaScript
OO in JavaScriptOO in JavaScript
OO in JavaScript
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
Lec2
Lec2Lec2
Lec2
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
Java script arrays
Java script arraysJava script arrays
Java script arrays
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 

Recently uploaded

costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Recently uploaded (20)

costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

Extending javascript part one

  • 2. Everythingshould beas simple as it is, but not simpler.  Albert Einstein
  • 3. Javascript is a loosely-typed language 
  • 4. Javascript is a loosely-typed language var x = 23; //number
  • 5. Javascript is a loosely-typed language var x = 23; //number x = "Apple"; //string
  • 6. Javascript is a loosely-typed language var x = 23; //number x = "Apple"; //string x = new Array(); //object
  • 7. Javascript is a loosely-typed language var x = 23; //number x = "Apple"; //string x = new Array(); //object Any thing can be assigned to x.
  • 8. What we are going to see in this presentation? 
  • 9. What we are going to see in this presentation? Creating type-safe collections like stackandqueue.
  • 10. What we are going to see in this presentation? Creating type-safe collections like stackandqueue.
  • 12. 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.
  • 13. 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); };
  • 14. Creating type-safe collections For help.. Create an array that contains the primitive types. var primitives = ["number", "string", "object", "boolean", "function"];
  • 15. Let's make our hands dirty 
  • 17. Stack Plan Create a new class named Stack.
  • 18. Stack Plan Create a new class named Stack. Encapsulate an instance of array in a property.
  • 19. 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.
  • 20. 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");
  • 21. Stack Plan For storing custom types pass the type itself to the constructor. var Employee = function() { }; //custom type var myStack = new Stack(Employee);
  • 22. 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.
  • 23. 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.
  • 24. 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.
  • 25. Stack Plan Create a method named getValue() to get the item at particular index.
  • 26. 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.
  • 27. 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.
  • 28. Stack Action Create a new class named Stack. var Stack = function(type){ }
  • 29. Stack Action Create a new class named Stack. var Stack = function(type){ } Function is a first class object in javascript.
  • 30. 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.
  • 31. 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.
  • 32. 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.
  • 33. 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 noconstructor that takes " + arguments.length + " arguments"); ...
  • 34. 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 noconstructor that takes " + arguments.length + " arguments"); ... If the constructor takes less or more than one parameter throw error.
  • 35. Stack Action Check 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; else throw new Error("Invalid Type"); ...
  • 36. Stack Action Check 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; else throw new Error("Invalid Type"); ... If the type is primitive set isPrimitive property to true else false.
  • 37. 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.
  • 38. 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.
  • 39. 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.
  • 40. Stack Action Set the constructor for the Stack. Stack.prototype.constructor = Stack;
  • 41. 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.
  • 42. Stack Action Create a method named push() to push items into the collection. Stack.prototype.push = function(){ }
  • 43. 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.
  • 44. 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
  • 45. 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.
  • 46. 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
  • 47. 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 internalarray.
  • 48. 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.
  • 49. 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; }
  • 50. Stack Action Create a method named getValue() to get the item at particular index. Stack.prototype.getValue = function(index){ return this.array[index]; }
  • 51. 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]; }
  • 52. Stack Complete 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; else throw new Error("Invalid Type"); this.type = type; this.array = new Array(); this.length = this.array.length; return this; }
  • 53. 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() == typeofarguments[i]) : (arguments[i] instanceof this.type); if(!isValid) throw new Error("Invalid Argument"); }
  • 54. 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]; }
  • 55. 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]; }
  • 56. It's time to test
  • 57. It's time to test Steps Start the firefox.
  • 58. It's time to test Steps Start the firefox. Open the firebug console window.
  • 59. It's time to test Steps Testing constructor. var myStack = new Stack();
  • 60. It's time to test Steps Testing constructor. var myStack = new Stack(); Error: There is no constructor that takes 0 arguments
  • 61. It's time to test Steps Testing constructor. var myStack = new Stack("string1");
  • 62. It's time to test Steps Testing constructor. var myStack = new Stack("string1"); Error: Invalid Type
  • 63. It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push(1);
  • 64. It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push(1); Error: Invalid Argument
  • 65. It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push("Apple");
  • 66. It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push("Apple"); 1
  • 67. It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push("Apple"); myStach.push("Orange", 3);
  • 68. It's time to test Steps Testing push(). var myStack = new Stack("string"); myStack.push("Apple"); myStach.push("Orange", 3); Error: Invalid Argument
  • 69. It's time to test Steps Testing push(). var Employee = function(name) { this.name = name };
  • 70. It's time to test Steps Testing push(). var Employee = function(name) { this.name = name }; var myStack = new Stack(Employee);
  • 71. It's time to test Steps Testing push(). var Employee = function(name) { this.name = name }; var myStack = new Stack(Employee); myStack.push("Apple");
  • 72. 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
  • 73. 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
  • 74. It's time to test Steps Testing getValue(). myStack.getValue(0);
  • 75. It's time to test Steps Testing getValue(). myStack.getValue(0); Object { name="Stephenson" }
  • 77. Queue Plan Most of the stuff are same like Stack except some things.
  • 78. Queue Plan Most of the stuff are same like Stack except some things. Change the names of push() and pop() as enter() and exit().
  • 79. 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. Changethe exit() implementation.
  • 80. Queue Action The implementation of all the methods except exit() is same as Stack. Let's ignore them in discussion.
  • 81. Queue Action Change the exit() implementation. Queue.prototype.exit = function(){ this.array.remove(0); this.length = this.array.length; return this.length; }
  • 82. 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.
  • 83. Queue Complete var 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; else throw new Error("Invalid Type"); this.type = type; this.array = new Array(); this.length = this.array.length; return this; }
  • 84. 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() == typeofarguments[i]) : (arguments[i] instanceof this.type); if(!isValid) throw new Error("Invalid Argument"); }
  • 85. 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]; }
  • 86. 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]; }
  • 87. It's time to test
  • 88. It's time to test Steps Testingexit(). var myQueue = new Queue("string");
  • 89. It's time to test Steps Testingexit(). var myQueue = new Queue("string"); myQueue.enter("RED", "BLUE", "GREEN");
  • 90. It's time to test Steps Testingexit(). var myQueue = new Queue("string"); myQueue.enter("RED", "BLUE", "GREEN"); myQueue.exit();
  • 91. It's time to test Steps Testingexit(). var myQueue = new Queue("string"); myQueue.enter("RED", "BLUE", "GREEN"); myQueue.exit();
  • 92. It's time to test Steps Testingexit(). myQueue.getValue(0);
  • 93. It's time to test Steps Testingexit(). myQueue.getValue(0); "BLUE"