SlideShare une entreprise Scribd logo
1  sur  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 

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 

Dernier (20)

Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 

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"