Slide 1

Slide 1 text

Object Oriented JavaScript Object Oriented JavaScript & & Prototype Chaining Prototype Chaining Narendra Sisodiya @nsisodiya

Slide 2

Slide 2 text

Narendra Sisodiya I am JavaScript Developer I develop Large Scale, Scalable, Single Page Applications @nsisodiya github.com/nsisodiya speakerdeck.com/nsisodiya

Slide 3

Slide 3 text

Agenda  Understand Object  Understand this keyword  Understand Object.create  Understand Object.prototype  Understand Prototype chaining  Understand Prototype Inheritance  with this and new keyword  without this and new keyword

Slide 4

Slide 4 text

Objects  var o = {}; // is equivalent to:  var o = Object.create(Object.prototype); An Object is dynamic collection of properties  Every properties is key-value pair  key is traditionally string, but with WeakMap, it can be any other object.

Slide 5

Slide 5 text

Getter/Setter/Delete  var o = {};  Set  o.id = 34  o.name = "Narendra"  Get  console.log(o.id)  console.log(o.name)  Delete  delete o.id  delete o.name  o["name"] = "Narendra" Dot Notation and Bracket Notation are exactly same

Slide 6

Slide 6 text

Object Literals

Slide 7

Slide 7 text

Object Literals

Slide 8

Slide 8 text

WHAT IF I TOLD YOU JavaScript do not have CLASSES

Slide 9

Slide 9 text

Surprise : There are no CLASSES In JavaScript An Object can extend from Another Object

Slide 10

Slide 10 text

● Is JavaScript object-oriented?  It has objects which can contain data and methods that act upon that data. Objects can contain other objects. It does not have classes, but it does have constructors which do what classes do, including acting as containers for class variables and methods. It does not have class- oriented inheritance, but it does have prototype-oriented inheritance.  By - Douglas Crockford

Slide 11

Slide 11 text

How ??? using Object.create employee object don't have name property but still employee.name works ! How ???

Slide 12

Slide 12 text

Object extending Object empoyee person Object.prototype

Slide 13

Slide 13 text

empoyee Array.prototype Object.prototype

Slide 14

Slide 14 text

hasOwnProperty

Slide 15

Slide 15 text

Now you know! Prototype Chaining for a property, first it will look into object, if unable to find, it will look into object.__proto__ , if unable to find, it will look into object.__proto__.proto__ and so on, until it find null. This is call Prototype Chaining

Slide 16

Slide 16 text

Over-riding properties !

Slide 17

Slide 17 text

Object Linking Diagrams employee job work tags __proto__ constructor Object.prototype toString __proto__ hasOwnProperty null : : function Object() { [native code] } person name place __proto__ constructor Longer the prototype chain, more the access time Array.prototype push __proto__ map : : employee.tags 1 length __proto__ constructor 0 function Array() { [native cod`e] }

Slide 18

Slide 18 text

But I cannot see multiple object ?

Slide 19

Slide 19 text

Here you are

Slide 20

Slide 20 text

Here you are Each Object point to same set of methods Advantage properties are public, easily accessible from outside Ex - p1.place Disadvantage

Slide 21

Slide 21 text

new Keyword - Alternate Syntax

Slide 22

Slide 22 text

Let compare, both syntax

Slide 23

Slide 23 text

Object Linking Diagrams Person.prototype setName __proto__ getName function Person() { } p2 name place __proto__ constructor Longer the prototype chain, more the access time function Object() { [native cod`e] } p1 name place __proto__ constructor Object.prototype toString __proto__ hasOwnProperty null : :

Slide 24

Slide 24 text

Understanding “this” keyword

Slide 25

Slide 25 text

Inheritance

Slide 26

Slide 26 text

Without new and this keyword

Slide 27

Slide 27 text

Inheritance

Slide 28

Slide 28 text

Inheritance

Slide 29

Slide 29 text

Questions?

Slide 30

Slide 30 text

Function at prototype chain & context eat: function (){ alert(this.name +" is eating"); } function eat is not part of object “narendra”, this.name When you run, narendra.eat(), eat() function of prototype chain will be executed with Execution Context == narendra, Every function executed with a context, narendra.eat() will be executed with context as “narendra” so inside eat function, value of this will be narendra narendra === this //true person.eat(); // Child of earth is eating person.eat.call(narendra); // Narendra Sisodiya is eating

Slide 31

Slide 31 text

What happen If I do not use “new” ? var Car = function(data){ console.dir(this); this.data = data; } var a = new Car(); var b = Car(); WITH NEW var Car = function(data){ //var this = new Object.create(Car.prototype); console.dir(this); this.data = data; // return this } Without new – value of this will be Window object

Slide 32

Slide 32 text

What happen If I do not use “new” ? var a = new Car(); var b = Car(); When you invoke constructor with new operator, function will be passed with a THIS variable which inherit from function.prototype

Slide 33

Slide 33 text

Module Pattern without new Operator (1st way) var Car = function(data) { this.data = data; } Car.prototype = { drive: function() { alert("Car is running"); return this; }, giveName: function(){ alert("The car name is " + this.data); return this; } }; var CarFactory = function(data){ return new Car(data); }