SlideShare a Scribd company logo
Advanced JavaScript: closures, prototypes, inheritance Stoyan Stefanov Ajax Experience, Boston 2008
About the presenter Yahoo! performance    team member YSlow 2.0 architect, dev Book author, open-source    contributor Blog:  http:// phpied.com
Before we start… Firebug console
Firebug console is a learning tool
Firebug console… Inspect the contents of objects by clicking on them Tab auto-complete, a.k.a cheatsheet Arrows ↑ and↓ Fiddle with any page
Any page…
Fiddle…
Objects
JavaScript data types primitive   and  objects number string boolean undefined null
What’s an object? a hash of key => value pairs if a key ( property ) happens to be a function, we can call it a  method
What’s an object? var  obj =  { shiny:  true , isShiny:  function () { return   this .shiny; } } ; obj.isShiny();  // true
Object literal notation {  Wrapped in curly braces  } , -delimited properties key : value pairs var obj = {a: 1, "b c d": 2};
Arrays
Arrays arrays are also objects auto-incremented properties
Arrays >>>  var  a = [1,3,2]; >>> a[0] 1 >>>  typeof  a "object"
Arrays array objects also get some cool properties... >>> a. length 3 ...and methods >>> a. sort () >>> a. join ( ' < ' ) &quot;1 < 2 < 3&quot;
Array literal notation var  array = [  &quot;Square&quot; ,  &quot;brackets&quot; ,   &quot;wrap&quot; ,  &quot;the&quot; ,   &quot;comma-delimited&quot; , &quot;elements&quot; ];
JSON JavaScript Object Notation Uses object and array literals Quotes required for properties {&quot;num&quot;:  1 , &quot;str&quot;:  &quot;abc&quot; , &quot;arr&quot;: [ 1 , 2 , 3 ]}
Functions
Functions functions are objects they have properties they have methods can de copied, deleted, augmented... special feature: invokable
Functions function  boo(what) { return  what; } or var  boo =  function (what) { return  what; } ;
Functions function  boo(what) { return  what; } or var  boo =  function   bootoo (what) { return  what; } ;
Functions are objects >>> boo.length 1 >>> boo.name &quot;bootoo&quot;
Functions are objects >>>  var  foo = boo; >>> foo( &quot;doodles&quot; ) &quot;doodles&quot; >>> foo. call ( null ,  &quot;moo!&quot; ); &quot;moo!&quot;
Return value all functions return a value if they don't explicitly, they return  undefined  implicitly functions can return other functions
Constructors
Constructors when invoked with  new , functions return an object known as  this you have a chance of modifying  this  before it's returned you can also return some other object
Constructor functions var  Person =  function (name) { this .name = name; this .speaks =  'fr' ; this .say = function() { return  &quot;Je m'appelle &quot;  +  this .name; }; };
An object created with constructor >>>  var  julien =  new  Person( &quot;Julien&quot; ); >>> julien.say(); &quot;Je m'appelle Julien&quot;
Constructor’s return value var  Person =  function (){ this .first =  &quot;Bruce&quot; ;  return  {last:  &quot;Wayne&quot; }; }; >>>  typeof new  Person().first &quot;undefined&quot; >>>  new  Person().last &quot;Wayne&quot;
Constructor’s return value var  Person =  function (){ this .first =  &quot;Bruce&quot; ;  return   &quot;Batman&quot; ; }; >>>  new  Person().first &quot;Bruce&quot;
Naming convention M yConstructor m yFunction
constructor  property >>>  function  Person(){}; >>>  var  jo =  new  Person(); >>> jo. constructor  === Person true
constructor  property >>>  var  o = {}; >>> o. constructor  === Object true >>> [1,2]. constructor  === Array true
Built-in constructor functions Object Array Function RegExp Number String Boolean Date Error, SyntaxError, ReferenceError…
var fn = new Function( 'a, b','return a+b'); var fn = function(a, b){ return a + b; } var re = new RegExp( '[a-z]', 'gmi'); var re = /[a-z]/gmi; var a = new Array(); var a = []; var o = new Object(); var o = {}; Not that Use this
Wrapper objects vs. primitive >>>  typeof new Number ( 1 ) &quot;object&quot; >>>  typeof   1 &quot;number&quot;
Primitives can act as objects >>>  &quot;test&quot; . length 4 >>> ( 123.456 ). toFixed ( 2 ) &quot;123.46&quot;
Prototype
prototype a property of the function objects >>>  var  boo =  function (){}; >>>  typeof  boo. prototype &quot;object&quot;
Prototypes can be augmented >>> boo. prototype .a =  1 ; >>> boo. prototype .sayAh =  function (){};
Prototypes can be overwritten >>> boo. prototype  = {a:  1 , b:  2 };
How is the prototype used? when a function is invoked as a constructor var  Person =  function (name) { this .name = name; }; Person. prototype .say =  function () { return this .name; }
>>>  var  dude =  new  Person( 'dude' ); >>> dude.name; &quot;dude&quot; >>> dude.say(); &quot;dude&quot; How is the prototype used?
say()  is a property of the  prototype  object but it behaves as if it's a property of the  dude  object can we tell the difference? How is the prototype used?
Own properties vs. prototype’s >>> dude. hasOwnProperty ( 'name' ); true >>> dude. hasOwnProperty ( 'say' ); false
isPrototypeOf() >>> Person. prototype . isPrototypeOf (dude); true >>>  Object . prototype . isPrototypeOf (dude); true
__proto__ I, the  dude , have a secret link to the prototype of the constructor that created me __proto__  is not directly exposed in all browsers
>>> dude. __proto__ . hasOwnProperty ( 'say' ) true >>> dude. prototype ???  // Trick question >>> dude. __proto__ . __proto__ . hasOwnProperty ( 'toString' ) true __proto__
The prototype chain
It’s alive! >>>  typeof  dude.numlegs &quot;undefined&quot; >>> Person. prototype .numlegs =  2 ; >>> dude.numlegs 2
Inheritance
Inheritance via the prototype >>>  var  Dad =  function (){ this .family =  &quot;Stefanov&quot; ;}; >>>  var  Kid =  function (){}; >>> Kid. prototype  =  new  Dad(); >>>  var  billy =  new  Kid(); >>> billy.family &quot;Stefanov&quot;
Inherit one more time >>>  var  GrandKid =  function (){}; >>> GrandKid. prototype  = billy; >>>  var  jill =  new  GrandKid(); >>> jill.family &quot;Stefanov&quot;
Inheritance… >>> jill. hasOwnProperty ( 'family' ) false >>> jill. __proto__ . hasOwnProperty ( 'family' ) false >>> jill. __proto__ . __proto__ . hasOwnProperty ( 'family' ) true
Inheritance… >>> billy.family =  'Idol' ; >>> jill.family; 'Idol' >>> jill. __proto__ . hasOwnProperty ( 'family' ); true >>>  delete  billy.family; >>> jill.family; 'Stefanov'
Side effect… >>> billy. constructor  === Kid false >>> billy. constructor  === Dad true
Side effect… easy to solve reset after inheritance >>> Kid. prototype . constructor  = Kid; >>> GrandKid. prototype . constructor  = GrandKid;
isPrototypeOf >>> billy. isPrototypeOf (jill) true >>> Kid. prototype . isPrototypeOf (jill) true
instanceof >>> jill  instanceof  GrandKid true >>> jill  instanceof  Kid true >>> jill  instanceof  Dad true
Classes? There are no classes in JavaScript Objects inherit from objects class ical inheritance is when we think of constructors as if they were classes
Classical inheritance function  Parent(){ this .name =  'parent' ;} Parent. prototype .getName =  function (){   return this .name; }; function  Child(){} inherit(Child, Parent);
Option 1 function  inherit(C, P) { C. prototype  =  new  P(); }
Option 2 function  inherit(C, P) { C. prototype  = P. prototype ; }
Option 3 function  inherit(C, P) { var  F =  function (){}; F. prototype  = P. prototype ; C. prototype  =  new  F(); }
Option 3 + super function  inherit(C, P) { var  F =  function (){}; F. prototype  = P. prototype ; C. prototype  =  new  F(); C.uber = P. prototype ; }
Option 3 + super + constructor reset function  inherit(C, P) { var  F =  function (){}; F. prototype  = P. prototype ; C. prototype  =  new  F(); C.uber = P. prototype ;  // super C. prototype . constructor  = C;  // reset }
Inheritance by copying properties After all, inheritance is all about code reuse function  extend(parent) { var  i, child = {}; for  (i  in  parent) { child[i] = parent[i]; } return  child; }
Inheritance by copying… >>>  var  parent = {a:  1 }; >>>  var  child = extend(parent); >>> child.a 1
Inheritance by copying… This was a shallow copy you can make a deep copy using recursion mixins / multiple inheritance
Prototypal inheritance as suggested by Douglas Crockford no class-like constructors involved objects inherit from objects via the prototype
Prototypal inheritance function  object(o) { function  F(){} F. prototype  = o; return new  F(); }
Prototypal inheritance >>>  var  parent = {a:  1 }; >>>  var  child = object(parent); >>> child.a; 1 >>> child. hasOwnProperty (a); false
Scope
No block scope >>> if ( true ) { var  inside_block =  1 ;} >>> inside_block 1
Function scope function  boo() { var  inboo =  true ; }
Global namespace every variable is global unless it's in a function and is declared with  var global namespace should be kept clean to avoid naming collisions function scope can help
Self-executable functions for  one-off tasks ( function (){ var  a =  1 ; var  b =  2 ; alert (a + b); } )()
Closures
Photo credit:  NASA
 
Closure example #1 function  outer(){ var  local =  1 ; return   function (){ return  local; }; }
example #1… >>>  var  inner = outer() >>> inner() 1
Closure example #2 var  inner; function  outer(){ var  local =  1 ; inner =  function (){ return  local; }; }
example #2… >>>  typeof  inner &quot;undefined&quot; >>> outer() >>> inner() 1
Closure example #3 function  makePlus(arg) { var  n =  function (){ return  arg; }; arg++; return  n; }
example #3… >>>  var  getValue = makePlus( 1234 ); >>> getValue() 1235
Closure #4 – in a loop function  make() { var  i, a = []; for (i =  0 ; i <  3 ; i++) { a[i] =  function (){ return  i; } } return  a; }
Closure #4 test - oops >>>  var  funcs = make(); >>> funcs[ 0 ](); 3 >>> funcs[ 1 ](); 3 >>> funcs[ 2 ](); 3
Closure #4 – corrected function  make() { var  i, a = []; for (i =  0 ; i <  3 ; i++) { a[i] = ( function (local){ return   function (){ return  local;} })(i) } return  a; }
Getter/Setter var  getValue, setValue; ( function () { var  secret =  0 ; getValue =  function (){ return  secret; }; setValue =  function (v){ secret = v; }; })() // usage >>> getValue() 0 >>> setValue( 123 ) >>> getValue() 123
Iterator function  setup(x) { var  i =  0 ; return   function (){ return  x[i++]; }; }
Iterator usage >>>  var  next = setup([ 'a' ,  'b' ,  'c' ]); >>> next() 'a' >>> next() 'b'
Loop through DOM elements - wrong // all elements will alert 5 for (var i = 1; i < 5; i++ ){ document.getElementById('btn'+i).onclick = function(){ alert(i); }; }
Loop through DOM elements - correct // first element alerts 1, second 2,... for (var i = 1; i < 5; i++ ){ document.getElementById('btn'+i).onclick = ( function( i ){ return function(){ alert(i); }; } )(i) }
Wrapping up… How to tell what’s going on? typeof, instanceof, isPrototypeOf()…
>>>  typeof   variable typeof  is an operator, not a function Not  typeof(variable)  even if it works Returns a string, one of: &quot;string&quot;, &quot;number&quot;, &quot;boolean&quot;,  &quot;undefined&quot;, &quot;object&quot;, &quot;function&quot;
typeof if  ( typeof  whatever ===  &quot;undefined&quot;)  { // whatever is not defined } if  (whatever == undefined) { // hmm, not so sure }
>>> obj  instanceof   MyConstructor Not  instanceof() Returns  true  |  false true  for all constructors up the chain
>>> obj. constructor Points to the constructor function used to create this  obj
>>> obj. isPrototypeOf (child_obj) Respects the prototype chain
>>> obj. hasOwnProperty ( &quot;prop&quot; ) Own properties vs. properties of the prototype
obj. propertyIsEnumerable ( &quot;prop&quot; ) Will it show up in a  for-in  loop Caution: enumerable properties of the prototype will return  false  but still show up in the for-in loop
Wrapping up… What did we learn today?
Objects JavaScript has a few primitive types, everything else is an object Objects are hashes Arrays are objects
Functions Functions are objects, only invokable call()  and  apply()  methods prototype  property
Prototype Functions have a  prototype  property which is an object Useful with Constructor functions
Constructor A function meant to be called with  new Returns an object
Class No such thing in JavaScript
Inheritance Prototypal Class ical … and approximately 101 other ways and variations
Scope Lexical function scope
Closure When a variable leaves its function scope
Thank you! Stoyan Stefanov http://www.phpied.com

More Related Content

What's hot (20)

Demystifying Prototypes
Demystifying PrototypesDemystifying Prototypes
Demystifying Prototypes
Dmitry Baranovskiy
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
Edureka!
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
Javascript Basic
Javascript BasicJavascript Basic
Javascript Basic
Kang-min Liu
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
Apache Click
Apache ClickApache Click
Apache Click
오석 한
 
Datatype in JavaScript
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScript
Rajat Saxena
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
Alaref Abushaala
 
Django admin site 커스텀하여 적극적으로 활용하기
Django admin site 커스텀하여 적극적으로 활용하기Django admin site 커스텀하여 적극적으로 활용하기
Django admin site 커스텀하여 적극적으로 활용하기
영우 박
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Lovely Professional University
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
Hassan Dar
 
Java script ppt
Java script pptJava script ppt
Java script ppt
The Health and Social Care Information Centre
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
Sherihan Anver
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Scott Leberknight
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
Doeun KOCH
 
Array in Java
Array in JavaArray in Java
Array in Java
Shehrevar Davierwala
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
Edureka!
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
Datatype in JavaScript
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScript
Rajat Saxena
 
Django admin site 커스텀하여 적극적으로 활용하기
Django admin site 커스텀하여 적극적으로 활용하기Django admin site 커스텀하여 적극적으로 활용하기
Django admin site 커스텀하여 적극적으로 활용하기
영우 박
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
Hassan Dar
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
Doeun KOCH
 

Viewers also liked (15)

Reactive JavaScript
Reactive JavaScriptReactive JavaScript
Reactive JavaScript
Stoyan Stefanov
 
2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance
pedro.carvalho
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
Jussi Pohjolainen
 
Javascript foundations: Inheritance
Javascript foundations: InheritanceJavascript foundations: Inheritance
Javascript foundations: Inheritance
John Hunter
 
New ES6 Hotness
New ES6 HotnessNew ES6 Hotness
New ES6 Hotness
Pawel Szymczykowski
 
JavaScript Inheritence
JavaScript  InheritenceJavaScript  Inheritence
JavaScript Inheritence
Pawel Szymczykowski
 
Basic inheritance in JavaScript
Basic inheritance in JavaScriptBasic inheritance in JavaScript
Basic inheritance in JavaScript
Brian Moschel
 
JavaScript: The prototype Property
JavaScript: The prototype PropertyJavaScript: The prototype Property
JavaScript: The prototype Property
Guillermo Paz
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
JavaScript Prototype and Module Pattern
JavaScript Prototype and Module PatternJavaScript Prototype and Module Pattern
JavaScript Prototype and Module Pattern
Narendra Sisodiya
 
JavaScript OOPs
JavaScript OOPsJavaScript OOPs
JavaScript OOPs
Johnson Chou
 
ASP.NET Core: The best of the new bits
ASP.NET Core: The best of the new bitsASP.NET Core: The best of the new bits
ASP.NET Core: The best of the new bits
Ken Cenerelli
 
Prototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScriptPrototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScript
Sunny Sharma
 
ASP.NET Core 1.0 Overview
ASP.NET Core 1.0 OverviewASP.NET Core 1.0 Overview
ASP.NET Core 1.0 Overview
Shahed Chowdhuri
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance
pedro.carvalho
 
Javascript foundations: Inheritance
Javascript foundations: InheritanceJavascript foundations: Inheritance
Javascript foundations: Inheritance
John Hunter
 
Basic inheritance in JavaScript
Basic inheritance in JavaScriptBasic inheritance in JavaScript
Basic inheritance in JavaScript
Brian Moschel
 
JavaScript: The prototype Property
JavaScript: The prototype PropertyJavaScript: The prototype Property
JavaScript: The prototype Property
Guillermo Paz
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
JavaScript Prototype and Module Pattern
JavaScript Prototype and Module PatternJavaScript Prototype and Module Pattern
JavaScript Prototype and Module Pattern
Narendra Sisodiya
 
ASP.NET Core: The best of the new bits
ASP.NET Core: The best of the new bitsASP.NET Core: The best of the new bits
ASP.NET Core: The best of the new bits
Ken Cenerelli
 
Prototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScriptPrototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScript
Sunny Sharma
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
Ad

Similar to Advanced JavaScript (20)

Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
JavaScript In Object Oriented Way
JavaScript In Object Oriented WayJavaScript In Object Oriented Way
JavaScript In Object Oriented Way
Borey Lim
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
relay12
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
Manikanda kumar
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: Prototypes
Vernon Kesner
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
David Atchley
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
deannalagason
 
Prototype
PrototypePrototype
Prototype
Aditya Gaur
 
Javascript Object Oriented Programming
Javascript Object Oriented ProgrammingJavascript Object Oriented Programming
Javascript Object Oriented Programming
Bunlong Van
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
Julie Iskander
 
Js objects
Js objectsJs objects
Js objects
anubavam-techkt
 
Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02
plutoone TestTwo
 
JavaScript Survival Guide
JavaScript Survival GuideJavaScript Survival Guide
JavaScript Survival Guide
Giordano Scalzo
 
JavaScript Essentials
JavaScript EssentialsJavaScript Essentials
JavaScript Essentials
Triphon Statkov
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
LearningTech
 
Typescript barcelona
Typescript barcelonaTypescript barcelona
Typescript barcelona
Christoffer Noring
 
Java Script Workshop
Java Script WorkshopJava Script Workshop
Java Script Workshop
Dmitry Baranovskiy
 
The many facets of code reuse in JavaScript
The many facets of code reuse in JavaScriptThe many facets of code reuse in JavaScript
The many facets of code reuse in JavaScript
Leonardo Borges
 
Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
Rodica Dada
 
OO in JavaScript
OO in JavaScriptOO in JavaScript
OO in JavaScript
Gunjan Kumar
 
Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
JavaScript In Object Oriented Way
JavaScript In Object Oriented WayJavaScript In Object Oriented Way
JavaScript In Object Oriented Way
Borey Lim
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
relay12
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: Prototypes
Vernon Kesner
 
Javascript Object Oriented Programming
Javascript Object Oriented ProgrammingJavascript Object Oriented Programming
Javascript Object Oriented Programming
Bunlong Van
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
Julie Iskander
 
Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02
plutoone TestTwo
 
JavaScript Survival Guide
JavaScript Survival GuideJavaScript Survival Guide
JavaScript Survival Guide
Giordano Scalzo
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
LearningTech
 
The many facets of code reuse in JavaScript
The many facets of code reuse in JavaScriptThe many facets of code reuse in JavaScript
The many facets of code reuse in JavaScript
Leonardo Borges
 
Ad

More from Stoyan Stefanov (20)

YSlow hacking
YSlow hackingYSlow hacking
YSlow hacking
Stoyan Stefanov
 
Liking performance
Liking performanceLiking performance
Liking performance
Stoyan Stefanov
 
JavaScript Performance Patterns
JavaScript Performance PatternsJavaScript Performance Patterns
JavaScript Performance Patterns
Stoyan Stefanov
 
JavaScript performance patterns
JavaScript performance patternsJavaScript performance patterns
JavaScript performance patterns
Stoyan Stefanov
 
High Performance Social Plugins
High Performance Social PluginsHigh Performance Social Plugins
High Performance Social Plugins
Stoyan Stefanov
 
Social Button BFFs
Social Button BFFsSocial Button BFFs
Social Button BFFs
Stoyan Stefanov
 
JavaScript навсякъде
JavaScript навсякъдеJavaScript навсякъде
JavaScript навсякъде
Stoyan Stefanov
 
JavaScript is everywhere
JavaScript is everywhereJavaScript is everywhere
JavaScript is everywhere
Stoyan Stefanov
 
JavaScript shell scripting
JavaScript shell scriptingJavaScript shell scripting
JavaScript shell scripting
Stoyan Stefanov
 
WPO @ PubCon 2010
WPO @ PubCon 2010WPO @ PubCon 2010
WPO @ PubCon 2010
Stoyan Stefanov
 
Progressive Downloads and Rendering - take #2
Progressive Downloads and Rendering - take #2Progressive Downloads and Rendering - take #2
Progressive Downloads and Rendering - take #2
Stoyan Stefanov
 
Progressive Downloads and Rendering
Progressive Downloads and RenderingProgressive Downloads and Rendering
Progressive Downloads and Rendering
Stoyan Stefanov
 
Performance patterns
Performance patternsPerformance patterns
Performance patterns
Stoyan Stefanov
 
Voices that matter: High Performance Web Sites
Voices that matter: High Performance Web SitesVoices that matter: High Performance Web Sites
Voices that matter: High Performance Web Sites
Stoyan Stefanov
 
Psychology of performance
Psychology of performancePsychology of performance
Psychology of performance
Stoyan Stefanov
 
3-in-1 YSlow
3-in-1 YSlow3-in-1 YSlow
3-in-1 YSlow
Stoyan Stefanov
 
CSS and image optimization
CSS and image optimizationCSS and image optimization
CSS and image optimization
Stoyan Stefanov
 
High-performance DOM scripting
High-performance DOM scriptingHigh-performance DOM scripting
High-performance DOM scripting
Stoyan Stefanov
 
The business of performance
The business of performanceThe business of performance
The business of performance
Stoyan Stefanov
 
JavaScript Patterns
JavaScript PatternsJavaScript Patterns
JavaScript Patterns
Stoyan Stefanov
 
JavaScript Performance Patterns
JavaScript Performance PatternsJavaScript Performance Patterns
JavaScript Performance Patterns
Stoyan Stefanov
 
JavaScript performance patterns
JavaScript performance patternsJavaScript performance patterns
JavaScript performance patterns
Stoyan Stefanov
 
High Performance Social Plugins
High Performance Social PluginsHigh Performance Social Plugins
High Performance Social Plugins
Stoyan Stefanov
 
JavaScript навсякъде
JavaScript навсякъдеJavaScript навсякъде
JavaScript навсякъде
Stoyan Stefanov
 
JavaScript is everywhere
JavaScript is everywhereJavaScript is everywhere
JavaScript is everywhere
Stoyan Stefanov
 
JavaScript shell scripting
JavaScript shell scriptingJavaScript shell scripting
JavaScript shell scripting
Stoyan Stefanov
 
Progressive Downloads and Rendering - take #2
Progressive Downloads and Rendering - take #2Progressive Downloads and Rendering - take #2
Progressive Downloads and Rendering - take #2
Stoyan Stefanov
 
Progressive Downloads and Rendering
Progressive Downloads and RenderingProgressive Downloads and Rendering
Progressive Downloads and Rendering
Stoyan Stefanov
 
Voices that matter: High Performance Web Sites
Voices that matter: High Performance Web SitesVoices that matter: High Performance Web Sites
Voices that matter: High Performance Web Sites
Stoyan Stefanov
 
Psychology of performance
Psychology of performancePsychology of performance
Psychology of performance
Stoyan Stefanov
 
CSS and image optimization
CSS and image optimizationCSS and image optimization
CSS and image optimization
Stoyan Stefanov
 
High-performance DOM scripting
High-performance DOM scriptingHigh-performance DOM scripting
High-performance DOM scripting
Stoyan Stefanov
 
The business of performance
The business of performanceThe business of performance
The business of performance
Stoyan Stefanov
 

Recently uploaded (20)

Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
 
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Aaryan Kansari
 
The case for on-premises AI
The case for on-premises AIThe case for on-premises AI
The case for on-premises AI
Principled Technologies
 
Securiport - A Border Security Company
Securiport  -  A Border Security CompanySecuriport  -  A Border Security Company
Securiport - A Border Security Company
Securiport
 
SDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhereSDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhere
Adtran
 
Palo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity FoundationPalo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity Foundation
VICTOR MAESTRE RAMIREZ
 
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure ModesCognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Dr. Tathagat Varma
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
UiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build PipelinesUiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build Pipelines
UiPathCommunity
 
Supercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMsSupercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMs
Francesco Corti
 
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 ADr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr. Jimmy Schwarzkopf
 
New Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDBNew Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDB
ScyllaDB
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Lorenzo Miniero
 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
 
European Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility TestingEuropean Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility Testing
Julia Undeutsch
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
 
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Aaryan Kansari
 
Securiport - A Border Security Company
Securiport  -  A Border Security CompanySecuriport  -  A Border Security Company
Securiport - A Border Security Company
Securiport
 
SDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhereSDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhere
Adtran
 
Palo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity FoundationPalo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity Foundation
VICTOR MAESTRE RAMIREZ
 
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure ModesCognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Dr. Tathagat Varma
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
UiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build PipelinesUiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build Pipelines
UiPathCommunity
 
Supercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMsSupercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMs
Francesco Corti
 
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 ADr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr. Jimmy Schwarzkopf
 
New Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDBNew Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDB
ScyllaDB
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Lorenzo Miniero
 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
 
European Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility TestingEuropean Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility Testing
Julia Undeutsch
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 

Advanced JavaScript

  • 1. Advanced JavaScript: closures, prototypes, inheritance Stoyan Stefanov Ajax Experience, Boston 2008
  • 2. About the presenter Yahoo! performance team member YSlow 2.0 architect, dev Book author, open-source contributor Blog: http:// phpied.com
  • 3. Before we start… Firebug console
  • 4. Firebug console is a learning tool
  • 5. Firebug console… Inspect the contents of objects by clicking on them Tab auto-complete, a.k.a cheatsheet Arrows ↑ and↓ Fiddle with any page
  • 9. JavaScript data types primitive and objects number string boolean undefined null
  • 10. What’s an object? a hash of key => value pairs if a key ( property ) happens to be a function, we can call it a method
  • 11. What’s an object? var obj = { shiny: true , isShiny: function () { return this .shiny; } } ; obj.isShiny(); // true
  • 12. Object literal notation { Wrapped in curly braces } , -delimited properties key : value pairs var obj = {a: 1, &quot;b c d&quot;: 2};
  • 14. Arrays arrays are also objects auto-incremented properties
  • 15. Arrays >>> var a = [1,3,2]; >>> a[0] 1 >>> typeof a &quot;object&quot;
  • 16. Arrays array objects also get some cool properties... >>> a. length 3 ...and methods >>> a. sort () >>> a. join ( ' < ' ) &quot;1 < 2 < 3&quot;
  • 17. Array literal notation var array = [ &quot;Square&quot; , &quot;brackets&quot; , &quot;wrap&quot; , &quot;the&quot; , &quot;comma-delimited&quot; , &quot;elements&quot; ];
  • 18. JSON JavaScript Object Notation Uses object and array literals Quotes required for properties {&quot;num&quot;: 1 , &quot;str&quot;: &quot;abc&quot; , &quot;arr&quot;: [ 1 , 2 , 3 ]}
  • 20. Functions functions are objects they have properties they have methods can de copied, deleted, augmented... special feature: invokable
  • 21. Functions function boo(what) { return what; } or var boo = function (what) { return what; } ;
  • 22. Functions function boo(what) { return what; } or var boo = function bootoo (what) { return what; } ;
  • 23. Functions are objects >>> boo.length 1 >>> boo.name &quot;bootoo&quot;
  • 24. Functions are objects >>> var foo = boo; >>> foo( &quot;doodles&quot; ) &quot;doodles&quot; >>> foo. call ( null , &quot;moo!&quot; ); &quot;moo!&quot;
  • 25. Return value all functions return a value if they don't explicitly, they return undefined implicitly functions can return other functions
  • 27. Constructors when invoked with new , functions return an object known as this you have a chance of modifying this before it's returned you can also return some other object
  • 28. Constructor functions var Person = function (name) { this .name = name; this .speaks = 'fr' ; this .say = function() { return &quot;Je m'appelle &quot; + this .name; }; };
  • 29. An object created with constructor >>> var julien = new Person( &quot;Julien&quot; ); >>> julien.say(); &quot;Je m'appelle Julien&quot;
  • 30. Constructor’s return value var Person = function (){ this .first = &quot;Bruce&quot; ; return {last: &quot;Wayne&quot; }; }; >>> typeof new Person().first &quot;undefined&quot; >>> new Person().last &quot;Wayne&quot;
  • 31. Constructor’s return value var Person = function (){ this .first = &quot;Bruce&quot; ; return &quot;Batman&quot; ; }; >>> new Person().first &quot;Bruce&quot;
  • 32. Naming convention M yConstructor m yFunction
  • 33. constructor property >>> function Person(){}; >>> var jo = new Person(); >>> jo. constructor === Person true
  • 34. constructor property >>> var o = {}; >>> o. constructor === Object true >>> [1,2]. constructor === Array true
  • 35. Built-in constructor functions Object Array Function RegExp Number String Boolean Date Error, SyntaxError, ReferenceError…
  • 36. var fn = new Function( 'a, b','return a+b'); var fn = function(a, b){ return a + b; } var re = new RegExp( '[a-z]', 'gmi'); var re = /[a-z]/gmi; var a = new Array(); var a = []; var o = new Object(); var o = {}; Not that Use this
  • 37. Wrapper objects vs. primitive >>> typeof new Number ( 1 ) &quot;object&quot; >>> typeof 1 &quot;number&quot;
  • 38. Primitives can act as objects >>> &quot;test&quot; . length 4 >>> ( 123.456 ). toFixed ( 2 ) &quot;123.46&quot;
  • 40. prototype a property of the function objects >>> var boo = function (){}; >>> typeof boo. prototype &quot;object&quot;
  • 41. Prototypes can be augmented >>> boo. prototype .a = 1 ; >>> boo. prototype .sayAh = function (){};
  • 42. Prototypes can be overwritten >>> boo. prototype = {a: 1 , b: 2 };
  • 43. How is the prototype used? when a function is invoked as a constructor var Person = function (name) { this .name = name; }; Person. prototype .say = function () { return this .name; }
  • 44. >>> var dude = new Person( 'dude' ); >>> dude.name; &quot;dude&quot; >>> dude.say(); &quot;dude&quot; How is the prototype used?
  • 45. say() is a property of the prototype object but it behaves as if it's a property of the dude object can we tell the difference? How is the prototype used?
  • 46. Own properties vs. prototype’s >>> dude. hasOwnProperty ( 'name' ); true >>> dude. hasOwnProperty ( 'say' ); false
  • 47. isPrototypeOf() >>> Person. prototype . isPrototypeOf (dude); true >>> Object . prototype . isPrototypeOf (dude); true
  • 48. __proto__ I, the dude , have a secret link to the prototype of the constructor that created me __proto__ is not directly exposed in all browsers
  • 49. >>> dude. __proto__ . hasOwnProperty ( 'say' ) true >>> dude. prototype ??? // Trick question >>> dude. __proto__ . __proto__ . hasOwnProperty ( 'toString' ) true __proto__
  • 51. It’s alive! >>> typeof dude.numlegs &quot;undefined&quot; >>> Person. prototype .numlegs = 2 ; >>> dude.numlegs 2
  • 53. Inheritance via the prototype >>> var Dad = function (){ this .family = &quot;Stefanov&quot; ;}; >>> var Kid = function (){}; >>> Kid. prototype = new Dad(); >>> var billy = new Kid(); >>> billy.family &quot;Stefanov&quot;
  • 54. Inherit one more time >>> var GrandKid = function (){}; >>> GrandKid. prototype = billy; >>> var jill = new GrandKid(); >>> jill.family &quot;Stefanov&quot;
  • 55. Inheritance… >>> jill. hasOwnProperty ( 'family' ) false >>> jill. __proto__ . hasOwnProperty ( 'family' ) false >>> jill. __proto__ . __proto__ . hasOwnProperty ( 'family' ) true
  • 56. Inheritance… >>> billy.family = 'Idol' ; >>> jill.family; 'Idol' >>> jill. __proto__ . hasOwnProperty ( 'family' ); true >>> delete billy.family; >>> jill.family; 'Stefanov'
  • 57. Side effect… >>> billy. constructor === Kid false >>> billy. constructor === Dad true
  • 58. Side effect… easy to solve reset after inheritance >>> Kid. prototype . constructor = Kid; >>> GrandKid. prototype . constructor = GrandKid;
  • 59. isPrototypeOf >>> billy. isPrototypeOf (jill) true >>> Kid. prototype . isPrototypeOf (jill) true
  • 60. instanceof >>> jill instanceof GrandKid true >>> jill instanceof Kid true >>> jill instanceof Dad true
  • 61. Classes? There are no classes in JavaScript Objects inherit from objects class ical inheritance is when we think of constructors as if they were classes
  • 62. Classical inheritance function Parent(){ this .name = 'parent' ;} Parent. prototype .getName = function (){ return this .name; }; function Child(){} inherit(Child, Parent);
  • 63. Option 1 function inherit(C, P) { C. prototype = new P(); }
  • 64. Option 2 function inherit(C, P) { C. prototype = P. prototype ; }
  • 65. Option 3 function inherit(C, P) { var F = function (){}; F. prototype = P. prototype ; C. prototype = new F(); }
  • 66. Option 3 + super function inherit(C, P) { var F = function (){}; F. prototype = P. prototype ; C. prototype = new F(); C.uber = P. prototype ; }
  • 67. Option 3 + super + constructor reset function inherit(C, P) { var F = function (){}; F. prototype = P. prototype ; C. prototype = new F(); C.uber = P. prototype ; // super C. prototype . constructor = C; // reset }
  • 68. Inheritance by copying properties After all, inheritance is all about code reuse function extend(parent) { var i, child = {}; for (i in parent) { child[i] = parent[i]; } return child; }
  • 69. Inheritance by copying… >>> var parent = {a: 1 }; >>> var child = extend(parent); >>> child.a 1
  • 70. Inheritance by copying… This was a shallow copy you can make a deep copy using recursion mixins / multiple inheritance
  • 71. Prototypal inheritance as suggested by Douglas Crockford no class-like constructors involved objects inherit from objects via the prototype
  • 72. Prototypal inheritance function object(o) { function F(){} F. prototype = o; return new F(); }
  • 73. Prototypal inheritance >>> var parent = {a: 1 }; >>> var child = object(parent); >>> child.a; 1 >>> child. hasOwnProperty (a); false
  • 74. Scope
  • 75. No block scope >>> if ( true ) { var inside_block = 1 ;} >>> inside_block 1
  • 76. Function scope function boo() { var inboo = true ; }
  • 77. Global namespace every variable is global unless it's in a function and is declared with var global namespace should be kept clean to avoid naming collisions function scope can help
  • 78. Self-executable functions for one-off tasks ( function (){ var a = 1 ; var b = 2 ; alert (a + b); } )()
  • 81.  
  • 82. Closure example #1 function outer(){ var local = 1 ; return function (){ return local; }; }
  • 83. example #1… >>> var inner = outer() >>> inner() 1
  • 84. Closure example #2 var inner; function outer(){ var local = 1 ; inner = function (){ return local; }; }
  • 85. example #2… >>> typeof inner &quot;undefined&quot; >>> outer() >>> inner() 1
  • 86. Closure example #3 function makePlus(arg) { var n = function (){ return arg; }; arg++; return n; }
  • 87. example #3… >>> var getValue = makePlus( 1234 ); >>> getValue() 1235
  • 88. Closure #4 – in a loop function make() { var i, a = []; for (i = 0 ; i < 3 ; i++) { a[i] = function (){ return i; } } return a; }
  • 89. Closure #4 test - oops >>> var funcs = make(); >>> funcs[ 0 ](); 3 >>> funcs[ 1 ](); 3 >>> funcs[ 2 ](); 3
  • 90. Closure #4 – corrected function make() { var i, a = []; for (i = 0 ; i < 3 ; i++) { a[i] = ( function (local){ return function (){ return local;} })(i) } return a; }
  • 91. Getter/Setter var getValue, setValue; ( function () { var secret = 0 ; getValue = function (){ return secret; }; setValue = function (v){ secret = v; }; })() // usage >>> getValue() 0 >>> setValue( 123 ) >>> getValue() 123
  • 92. Iterator function setup(x) { var i = 0 ; return function (){ return x[i++]; }; }
  • 93. Iterator usage >>> var next = setup([ 'a' , 'b' , 'c' ]); >>> next() 'a' >>> next() 'b'
  • 94. Loop through DOM elements - wrong // all elements will alert 5 for (var i = 1; i < 5; i++ ){ document.getElementById('btn'+i).onclick = function(){ alert(i); }; }
  • 95. Loop through DOM elements - correct // first element alerts 1, second 2,... for (var i = 1; i < 5; i++ ){ document.getElementById('btn'+i).onclick = ( function( i ){ return function(){ alert(i); }; } )(i) }
  • 96. Wrapping up… How to tell what’s going on? typeof, instanceof, isPrototypeOf()…
  • 97. >>> typeof variable typeof is an operator, not a function Not typeof(variable) even if it works Returns a string, one of: &quot;string&quot;, &quot;number&quot;, &quot;boolean&quot;, &quot;undefined&quot;, &quot;object&quot;, &quot;function&quot;
  • 98. typeof if ( typeof whatever === &quot;undefined&quot;) { // whatever is not defined } if (whatever == undefined) { // hmm, not so sure }
  • 99. >>> obj instanceof MyConstructor Not instanceof() Returns true | false true for all constructors up the chain
  • 100. >>> obj. constructor Points to the constructor function used to create this obj
  • 101. >>> obj. isPrototypeOf (child_obj) Respects the prototype chain
  • 102. >>> obj. hasOwnProperty ( &quot;prop&quot; ) Own properties vs. properties of the prototype
  • 103. obj. propertyIsEnumerable ( &quot;prop&quot; ) Will it show up in a for-in loop Caution: enumerable properties of the prototype will return false but still show up in the for-in loop
  • 104. Wrapping up… What did we learn today?
  • 105. Objects JavaScript has a few primitive types, everything else is an object Objects are hashes Arrays are objects
  • 106. Functions Functions are objects, only invokable call() and apply() methods prototype property
  • 107. Prototype Functions have a prototype property which is an object Useful with Constructor functions
  • 108. Constructor A function meant to be called with new Returns an object
  • 109. Class No such thing in JavaScript
  • 110. Inheritance Prototypal Class ical … and approximately 101 other ways and variations
  • 112. Closure When a variable leaves its function scope
  • 113. Thank you! Stoyan Stefanov http://www.phpied.com