advanced object oriented javascript (prototype, closure, scope, design patterns)

31
Object Oriented JavaScript By Kanakaraj Venkataswamy 28th & 29th November 2013

Upload: raja-kvk

Post on 11-May-2015

839 views

Category:

Technology


0 download

DESCRIPTION

Data type Scope Falsy IIF / Function as first class object Hoisting arguments Call / Apply Closure Name spacing Prototype

TRANSCRIPT

Page 1: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Object Oriented JavaScript

By Kanakaraj Venkataswamy

28th & 29th November 2013

Page 2: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Who am iTech Lead

Hertz ( www.hertz247.com )

email: [email protected]

twitter: @rajakvk

Blog: www.vkanakaraj.wordpress.com

Page 3: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Who are you?

• Name

• Hobby

• A secret about you

• Within 2 minutes

Page 4: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Agenda

• What is OO?

• Why OOJS?

• Important concepts

• Design pattern

• requirejs

Page 5: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

What is OO?

• Encapsulation

• Inheritance

• Polymorphism

• etc.

Page 6: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Why OOJS?

• Not only for form validation

• Complex

• Client side intensive

• Need better organisation

• Reuse

Page 7: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

background

• Interpreted, prototype based, dynamically typed, has first-class functions by Brendan Eich in 10 days for NetScape (now Mozilla) on Sep ‘1995.

• ECMA-262 (1-Jun‘97, 2-Jun’98, 3-Dec’99, 4-x, 5-Dec’09)

• Mocha, LiveScript, JavaScript

• V8, Chakra, Rhino, Carakan, Nitro, etc.

• Hosts: Browser, Acrobat Reader, Tools in Creative suit, etc

• Core, DOM, BOM

Page 8: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

object• key value pair

• key is string, value is ‘any’ javascript value

• var myObj = {}; // empty object

• var myObj = new Object(); // empty object

• var myObj = new Human(); // will be explained

• myObj = { fname: ‘raja’, lname: ‘kvk’ };

• console.log(myObj.fname); // raja

• myObj.project = ‘hertz’;

• console.log( myObj[‘project’] ); // hertz

• delete myObj.name

Page 9: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Important concepts• Data type

• Scope

• Falsy

• IIF / Function as first class object

• Hoisting

• arguments

• Call / Apply

• Closure

• Name spacing

• Prototype

Page 10: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Data Types• Primitive types

o numbero stringo booleano undefinedo null

• Everything else is objecto functiono arrayo date o regex, etc.

• Object is nothing but key/value pair

Page 11: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

ScopeScope in a programing language controls the visibility and lifetimes of variables and parameters.

• Global scope

• Function scope

• this

Page 12: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

FalsyThe following values are falsy

o falseo 0 (zero)o “” (empty string”)o nullo undefinedo NaN (not a number)

Everything else is truthy

Page 13: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Function - first class object

Function

• is an instance of Object type

• can have properties

• can have methods

• can be stored in a variable

• can be passed as parameter

• can be returned from a function

Page 14: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

IFFanonymous function

function(){

// code

}

Immediately invoked function

function(arg) {

alert(arg)

} (5)

Page 15: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

hoisting

• function declarations and variable

declarations are always moved (hoisted)

invisibly to the top of their containing scope

by the javascript interpreter.

• one var statement per scope at the top

Page 16: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

arguments

• an object available within all functions

• exists only inside function body

• not an Array but similar

• have length property

• does not have Array methods like pop

Page 17: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

call / apply

• call is used when you want to control the scope that will be used in the function called.

• http://jsfiddle.net/rajakvk/3Yp6D/

Page 18: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Definition: closure is the local variable for a function - kept alive after the function has returned

• Garbage collection

• Memory leak

closure

Page 19: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

name space

• Name spacing is a technique employed to avoid collisions with other objects or variables in the global scope.

• No built in support.

• var AppSpace = AppSpace || {};AppSpace.Mail = function(){};AppSpace.Video = function(){};

Page 20: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

prototype

• prototype is an object from which other objects inherit properties and methods

• Every function has a prototype by default

Page 21: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Great power comes with great responsibility

Page 22: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Take away

JavaScript is a flexible and expressive

language that should be written clearly and

concisely.

Page 23: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

require.jsAsynchronous Module Definition (AMD)

Page 24: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

The problem & solution

• Solves dependency management problem

• API: define(id, dependencies, factory)

• Keep execution order

• Loads text, CoffeeScript, template, etc.

• Minify & Build capability (optimization)

Page 25: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Design Pattern

Page 26: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Introduction

• Patterns are proven solutions to software

development problems.

• Patterns are reusable for similar problems.

• Creational, Structural, Behavioural

Page 27: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

What is covered here?Singleton - Creational

Module - Structural

Decorator - Structural

Observer - Behavioural

Page 28: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Singleton Pattern

• The singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.

• Namespacing, Grouping related methods and attributes together.

• Tightly coupled, Unit testing is difficult

• http://jsfiddle.net/rajakvk/gv644/

Page 29: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Module Pattern

• Loose definition: a way to provide both private and public encapsulation for classes.

• http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript

Page 30: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Decorator

• Decorators offered the ability to add behaviour to existing classes in a system dynamically.

• http://jsfiddle.net/rajakvk/69EdP/

Page 31: Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)

Observer Pattern

• Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

• http://www.dofactory.com/javascript-observer-pattern.aspx