Javascript

30
{ } Javascript (ECMA) Sandeepan Level: Advanced – Part 1

description

Content: Javascript, Class in javascript, prototyping Level: Intermediate - Advance

Transcript of Javascript

Page 1: Javascript

{ }Javascript (ECMA)

Sandeepan

Level: Advanced – Part 1

Page 2: Javascript

{ }Topics

• Types• Object• JSON• Defining Functions• Inheritance• Guidelines

Page 3: Javascript

{ }Topics

• Types• Object• JSON• Defining Functions• Inheritance• Guidelines

Page 4: Javascript

{ }{ “topic” : “Types” }

• Primitive– undefined– null– Boolean– Number– String

Page 5: Javascript

{ }Topics

• Types• Object• JSON• Defining Functions• Inheritance• Guidelines

Page 6: Javascript

{ }{ “topic” : “Object” }

• Few important points: – It is a collection of properties – has single prototype object– Prototype may be the null value

Page 7: Javascript

{ }{ “topic” : “Object” }

• constructor: – function object that creates and initializes objects– The value of constructors “prototype” property is a prototype object that is used to implement

inheritance and shared properties

• prototype:– object that provides shared properties for other objects

• native objects– object whose scemantics are fully defined by specification rather than host environment.– Eg: Object (constructor), Date, Math, parseInt, eval, string methods likeindexOf and replace, array

methods, ...

• built-in objects– independent of host environment

• host objects– object supplied by host environment– Eg: (assuming browser environment): window, document, location, history,

XMLHttpRequest, setTimeout, getElementsByTagName, querySelectorAll

Page 8: Javascript

{ }Topics

• Types• Object• JSON• Defining Functions• Inheritance• Guidelines

Page 9: Javascript

{ }{ “topic” : “JSON” }

Source: http://www.json.org/

Page 10: Javascript

{ }{ “topic” : “JSON” }

Source: http://www.json.org/

Page 11: Javascript

{ }{ “topic” : “JSON” }

Source: http://www.json.org/

Page 12: Javascript

{ }Topics

• Types• Object• JSON• Defining Functions• Inheritance• Guidelines

Page 13: Javascript

{ }{ “topic” : “Defining Functions” }

• Using a function

• instantiate

Source: http://www.phpied.com/

Page 14: Javascript

{ }{ “topic” : “Defining Functions” }

• Methods defined internally

• Methods added to the prototype

Source: http://www.phpied.com/

Page 15: Javascript

{ }{ “topic” : “Defining Functions” }

• Using object literals– For Object :

• var o = {};• instead of the "normal" way var o = new Object();

– For arrays you can do:• var a = [];• instead of the "normal" way var o = new Array();

– Usage

Source: http://www.phpied.com/

Page 16: Javascript

{ }{ “topic” : “Defining Functions” }

• Singleton using a function

• usage

Source: http://www.phpied.com/

Page 17: Javascript

{ }Topics

• Types• Object• JSON• Defining Functions• Inheritance• Guidelines

Page 18: Javascript

{ }{ “topic” : “inheritance” }

• Step 1: Create constructor function for Vehicle• Step 2: Create constructor function Car• Step 3: Set up dynamic inheritance• Step 4: Extending Car using prototype• Step 5: Creating a new Car• Step 6: Extend Vehicle using prototype

Source: http://www.klauskomenda.com

Page 19: Javascript

{ }{ “topic” : “inheritance” }

• Step 1: Create constructor function for Vehicle• Step 2: Create constructor function Car• Step 3: Set up dynamic inheritance• Step 4: Extending Car using prototype• Step 5: Creating a new Car• Step 6: Extend Vehicle using prototype

Source: http://www.klauskomenda.com

Page 20: Javascript

{ }{ “topic” : “inheritance” }

• Step 1: Create constructor function for Vehicle• Step 2: Create constructor function Car• Step 3: Set up dynamic inheritance• Step 4: Extending Car using prototype• Step 5: Creating a new Car• Step 6: Extend Vehicle using prototype

Source: http://www.klauskomenda.com

Page 21: Javascript

{ }{ “topic” : “inheritance” }

• Step 1: Create constructor function for Vehicle• Step 2: Create constructor function Car• Step 3: Set up dynamic inheritance• Step 4: Extending Car using prototype• Step 5: Creating a new Car• Step 6: Extend Vehicle using prototype

Source: http://www.klauskomenda.com

Page 22: Javascript

{ }{ “topic” : “inheritance” }

• Step 1: Create constructor function for Vehicle• Step 2: Create constructor function Car• Step 3: Set up dynamic inheritance• Step 4: Extending Car using prototype• Step 5: Creating a new Car• Step 6: Extend Vehicle using prototype

Source: http://www.klauskomenda.com

Page 23: Javascript

{ }{ “topic” : “inheritance” }

• Step 1: Create constructor function for Vehicle• Step 2: Create constructor function Car• Step 3: Set up dynamic inheritance• Step 4: Extending Car using prototype• Step 5: Creating a new Car• Step 6: Extend Vehicle using prototype

Source: http://www.klauskomenda.com

Page 24: Javascript

{ }{ “topic” : “inheritance” }

• Step 1: Create constructor function for Vehicle• Step 2: Create constructor function Car• Step 3: Set up dynamic inheritance• Step 4: Extending Car using prototype• Step 5: Creating a new Car• Step 6: Extend Vehicle using prototype

Source: http://www.klauskomenda.com

Page 25: Javascript

{ }{ “topic” : “inheritance” }

• Step 1: Create constructor function for Vehicle• Step 2: Create constructor function Car• Step 3: Set up dynamic inheritance• Step 4: Extending Car using prototype• Step 5: Creating a new Car• Step 6: Extend Vehicle using prototype

Source: http://www.klauskomenda.com

Page 26: Javascript

{ }{ “topic” : “inheritance” }

• Step 1: Create constructor function for Vehicle• Step 2: Create constructor function Car• Step 3: Set up dynamic inheritance• Step 4: Extending Car using prototype• Step 5: Creating a new Car• Step 6: Extend Vehicle using prototype

Source: http://www.klauskomenda.com

Page 27: Javascript

{ }Topics

• Types• Object• JSON• Defining Functions• Inheritance• Guidelines

Page 28: Javascript

{ }{ “topic” : “Guidelines” }• Namespace pollution – CAREFULL• Declaration with var – ALWAYS• Context/ scope – BEWARE• Separate JS file• No embedded JS code in HTML file

– (unless specific to single session)

• Use the scripts tag as late as possible in html file.• Coding

– Avoid mixins : as they always complicate the rendering engine and browser performance – Function: declare before usage– Indentation : 4 space– Line length : avoid more than 80 chars– Comments : preferable for each variables and be generous with comments

Page 29: Javascript

{ }Q & A

Page 30: Javascript

{ }

Thank You