Object Oriented JavaScript

16
BEING A JS NINJA: Object Oriented JavaScript - Suroor Wijdan

description

Many developers cast off JS as a suitable OOP language because they are so used to the class style of C# and Java. Many people don't realize that JavaScript supports inheritance. When you write object-oriented code it instantly gives you power; you can write code that can be re-used and that is encapsulated.

Transcript of Object Oriented JavaScript

Page 1: Object Oriented JavaScript

BEING A JS NINJA: Object Oriented JavaScript

- Suroor Wijdan

Page 2: Object Oriented JavaScript

Agenda

• Functions & Objects

• Prototype Based Programming

• Class Based - Prototype Based

• Method Overloading ?

• Some hacks

• Questions

• Exercises!

Page 3: Object Oriented JavaScript

Functions ( )

• A Simple JS function:

• function multiply(a, b){• return a * b;• };

• A simple assert function

• Note: A method is a function defined inside an object as its property!

Page 4: Object Oriented JavaScript

Functions ( )

• JavaScript only has ______ Scope.

• A function has access to all the variables and functions in the scope in which it is defined.

• Lets see an example!

• http://jsfiddle.net/suroorwijdan/dr3Bb/2/

Page 5: Object Oriented JavaScript

this & undefined

“JavaScript has two errors, this and undefined and when this is undefined, Oh Boy!” - Mikael Rogers

• http://jsfiddle.net/suroorwijdan/6gHjH/4/

Page 6: Object Oriented JavaScript

Functions ( )

• Closures

Closures are one of the most powerful weapons for a JS Ninja.

Closures allow a ninja to have access to a function scope with an exported inner function.

http://jsfiddle.net/suroorwijdan/nz9bM/9/

Page 7: Object Oriented JavaScript

Objects { }

• For a ninja almost everything is an object

• Creating an object:

• Using a Constructor - var obj = new Car();

• Using object initializer - var obj = {};

• Using Object.create()

http://jsfiddle.net/suroorwijdan/p5x2G/

Page 8: Object Oriented JavaScript

call and apply

• call() and apply() methods defined on functions allows a Ninja to change the context in which the function is executed.

http://jsfiddle.net/suroorwijdan/XzzU8/

Page 9: Object Oriented JavaScript

Prototype {} - {} - {}

“Most Confused Upon Part for budding Ninjas”

http://jsfiddle.net/suroorwijdan/h74gd/

Page 10: Object Oriented JavaScript

Class vs Prototype BasedClass Based (Java) Prototype Based (JavaScript)

Class and instance are distinct entities. All objects are instances.

Define a class with a class definition; instantiate a class with constructor methods.

Define and create a set of objects with constructor functions.

Create a single object with the new operator. Same

Construct an object hierarchy by using class definitions to define subclasses of existing classes.

Construct an object hierarchy by assigning an object as the prototype associated with a

constructor function.

Inherit properties by following the class chain. Inherit properties by following the prototype chain.

Class definition specifies all properties of all instances of a class. Cannot add properties dynamically at run time.

Constructor function or prototype specifies an initial set of properties. Can add or remove

properties dynamically to individual objects or to the entire set of objects.

Page 11: Object Oriented JavaScript

Method Overloading ?

Can we implement method overloading in JavaScript, if Yes, then How?? :|

http://jsfiddle.net/suroorwijdan/3xLtY/

Page 12: Object Oriented JavaScript

Some Hacks!

• Append new array to another array:

• var a = [4,5,6];• var b = [7,8,9];• Array.prototype.push.apply(a, b);

• Get TimeStamp with +• +new Date();

• Access Strings with [] just like charAt()• var a = “This is my trick”;• a[0] == ‘T’ //true;

Page 13: Object Oriented JavaScript

Some Hacks!

• Swap variable values:• var a = 10;• var b = 20;• [a,b] = [b,a]

And many more : http://code.google.com/p/jslibs/wiki/JavascriptTips

Page 14: Object Oriented JavaScript

Some More Concepts

• Memoization: “DIY”

• A memorize function is a higher order function which caches the result returned by the function passed to it.

Page 15: Object Oriented JavaScript

Exercises

• Implement Basic Inheritance with the example of Employee

• Add a forEach method in the prototype of Array

• Implement a function which multiplies the largest number with the first argument

Page 16: Object Oriented JavaScript

Let’s Connect

IntelliGrape Software (P) Ltd

SDF L-6, NSEZ,Noida Phase 2,IndiaPhone : +91-120-6493668Fax : +91-120-4207689Email : [email protected]

/company/intelligrape

/intelligrape.software

/IntelliGrape