SlideShare a Scribd company logo
Object-Oriented  JavaScript Stoyan Stefanov, Yahoo! Inc. Beijing, Dec 6 th , 2008
About the presenter Yahoo! Performance YSlow 2.0 smush.it  tool phpied.com  blog
First off…  the Firebug console
Firebug console as a learning tool
Console features 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 !== Java C-like syntax   Classes  
Data types A. Primitive: number –  1 ,  3 ,  1001 ,  11.12 ,  2e+3 string –  "a" ,  "stoyan" ,  "0" boolean –  true  |  false null undefined B. Objects everything else…
Objects hash tables  key: value
A simple object var  obj = {}; obj.name =  'my object' ; obj.shiny =  true ;
A simple object var  obj =  { shiny:  true , isShiny:  function () { return   this .shiny; } } ; obj.isShiny();  // true
Methods When a property is a function we can call it a method
Object literal notation Key-value pairs Comma-delimited Wrapped in curly braces {a: 1, b: "test"}
Arrays Arrays are objects too Auto-increment properties Some useful methods
Arrays >>>  var  a = [1,3,2]; >>> a[0] 1 >>>  typeof  a "object"
Array literal notation var  array = [  "Square" , "brackets" ,   "wrap" ,   "the" ,   "comma-delimited" , "elements" ];
JSON Object literals + array literals JavaScript Object Notation {"num":  1 , "str":  "abc" ,  "arr": [ 1 , 2 , 3 ]}
Constructors
Functions functions are objects they have properties they have methods can be copied, deleted, augmented... special feature: invokable
Function function  say(what) { return  what; }
Function var  say =  function (what) { return  what; } ;
Function var  say =  function   say (what) { return  what; } ;
Functions are objects >>> say.length 1 >>> say.name "boo"
Functions are objects >>>  var  tell = say; >>> tell( "doodles" ) "doodles" >>> tell. call ( null ,  "moo!" ); "moo!"
Return values All functions always return a value
Return values If a function doesn’t return a value explicitly, it returns  undefined
Return values Functions can return objects, including other functions
Constructors
Constructor functions when invoked with  new , functions return an object known as  this   you can modify  this  before it’s returned
Constructor functions var  Person =  function (name) { this .name = name; this .getName = function() { return  this .name; }; };
Using the constructor var  me =  new  Person( "Stoyan" ); me.getName();  // "Stoyan"
Constructors… …  are just functions
A 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 constructors Object Array Function RegExp Number String Boolean Date Error, SyntaxError, ReferenceError…
Use this Not that var o = {}; var o = new Object(); var a = []; var a = new Array(); var re = /[a-z]/gmi; var re = new RegExp( '[a-z]', 'gmi'); var fn = function(a, b){ return a + b; } var fn = new Function( 'a, b','return a+b');
Prototype
Prototype… …  is a property of the function objects
Prototype >>>  var  boo =  function (){}; >>>  typeof  boo. prototype "object"
Augmenting prototype >>> boo. prototype .a =  1 ; >>> boo. prototype .sayAh =  function (){};
Overwriting the prototype >>> boo. prototype  =  {a:  1 , b:  2 };
Use of the prototype The prototype is used when a function is called as a constructor
Prototype usage var  Person =  function (name) { this .name = name; }; Person. prototype .say =  function (){ return this .name; };
Prototype usage >>>  var  dude =  new  Person( 'dude' ); >>> dude.name; "dude" >>> dude.say(); "dude"
Prototype usage 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?
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__ The objects have a secret link to the prototype of the constructor that created them __proto__ is not directly exposed in all browsers
__proto__ >>> dude.__proto__. hasOwnProperty ( 'say' ) true >>> dude. prototype ???  // Trick question >>> dude.__proto__.__proto__. hasOwnProperty ( 'toString' ) true
Prototype chain
It’s a live chain >>>  typeof  dude.numlegs "undefined" >>> Person. prototype .numlegs =  2 ; >>> dude.numlegs 2
Inheritance
Parent constructor function  NormalObject() { this .name =  'normal' ; this .getName =  function () {   return   this .name;  }; }
Child constructor function  PreciousObject(){  this .shiny =  true ;  this .round =  true ;  }
The inheritance PreciousObject. prototype  =  new  NormalObject();
A child object var  crystal_ball =  new  PreciousObject(); crystal_ball.name =  'Crystal Ball.' ; crystal_ball.round;  // true crystal_ball.getName();  // "Crystal Ball."
Inheritance by copying
Two objects var  shiny = {  shiny:  true ,  round:  true   };  var  normal = {  name:  'name me' ,  getName:  function () { return this .name;  } };
extend() function  extend(parent, child) {  for  ( var  i  in  parent) {  child[i] = parent[i];  }  }
Inheritance extend(normal, shiny); shiny.getName();  // "name me"
Prototypal inheritance
Beget object function  object(o) { function  F(){} F. prototype  = o; return new  F(); }
Beget object >>>  var  parent = {a:  1 }; >>>  var  child = object(parent); >>> child.a; 1 >>> child. hasOwnProperty (a); false
Wrapping up…
Objects Everything is an object (except a few primitive types) Objects are hashes Arrays are objects
Functions Functions are objects Only invokable Methods: call(), apply() Properties: length, name, prototype
Prototype Property of the function objects It’s 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 Class ical Prototypal By copying …  and many other variants
Stoyan Stefanov, http://phpied.com [email_address]

More Related Content

What's hot (20)

PDF
Java Script Best Practices
Enrique Juan de Dios
 
PDF
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
PPT
JavaScript In Object Oriented Way
Borey Lim
 
PDF
Advanced javascript
Doeun KOCH
 
PDF
JavaScript - From Birth To Closure
Robert Nyman
 
PPTX
Object Oriented Programming In JavaScript
Forziatech
 
PPT
JavaScript Basics
Mats Bryntse
 
PPTX
Javascript basics for automation testing
Vikas Thange
 
PPT
A Deeper look into Javascript Basics
Mindfire Solutions
 
PDF
Powerful JavaScript Tips and Best Practices
Dragos Ionita
 
PDF
Prototype
Aditya Gaur
 
PDF
Core concepts-javascript
Prajwala Manchikatla
 
PPTX
5 Tips for Better JavaScript
Todd Anglin
 
PPTX
Javascript Prototype Visualized
军 沈
 
PDF
Basics of JavaScript
Bala Narayanan
 
ODP
Javascript
theacadian
 
PDF
JavaScript Patterns
Stoyan Stefanov
 
PDF
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
KEY
JavaScript Growing Up
David Padbury
 
PDF
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
Java Script Best Practices
Enrique Juan de Dios
 
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
JavaScript In Object Oriented Way
Borey Lim
 
Advanced javascript
Doeun KOCH
 
JavaScript - From Birth To Closure
Robert Nyman
 
Object Oriented Programming In JavaScript
Forziatech
 
JavaScript Basics
Mats Bryntse
 
Javascript basics for automation testing
Vikas Thange
 
A Deeper look into Javascript Basics
Mindfire Solutions
 
Powerful JavaScript Tips and Best Practices
Dragos Ionita
 
Prototype
Aditya Gaur
 
Core concepts-javascript
Prajwala Manchikatla
 
5 Tips for Better JavaScript
Todd Anglin
 
Javascript Prototype Visualized
军 沈
 
Basics of JavaScript
Bala Narayanan
 
Javascript
theacadian
 
JavaScript Patterns
Stoyan Stefanov
 
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
JavaScript Growing Up
David Padbury
 
Ten useful JavaScript tips & best practices
Ankit Rastogi
 

Viewers also liked (18)

PDF
Javascript Best Practices
Christian Heilmann
 
KEY
Object oriented javascript
Garrison Locke
 
PPT
ООП в JavaScript
enterra-inc
 
PDF
Turning to the client side
Lea Verou
 
ODP
JavaScript global object, execution contexts & closures
HDR1001
 
PPTX
Grunt
Dohoon Kim
 
ODP
Untitled 1
guestf218e5
 
PDF
The Wearable Machine
Steven Casey
 
DOCX
These companies are very good eficientando their innovation processes
Shelly Miller Moore
 
PPT
Making Beer Money from your Travel Blog
Heartwood Digital
 
PDF
طباعة التقرير - إختبر درجة إبداعك احمد الذهب
Ahmed Dahab
 
DOCX
Grandmas Recipes by Wendy Pang
Wendy Pang
 
DOCX
Matriz de valoracion pid y aamtic
Polo Apolo
 
PDF
Hakukoneet ja mittaristo #digisawotta
Aki Karkulahti
 
DOCX
Mapa conceptual. GESTIÓN DE PROYECTO
Marcela Leon
 
PDF
Plan lector 4 todos somos iguales
Luis Miguel Galiano Velasquez
 
PPTX
5 pen pc tecnology
manjushapdk
 
PDF
How can communities shape economic development and create quality jobs
Urban Habitat
 
Javascript Best Practices
Christian Heilmann
 
Object oriented javascript
Garrison Locke
 
ООП в JavaScript
enterra-inc
 
Turning to the client side
Lea Verou
 
JavaScript global object, execution contexts & closures
HDR1001
 
Grunt
Dohoon Kim
 
Untitled 1
guestf218e5
 
The Wearable Machine
Steven Casey
 
These companies are very good eficientando their innovation processes
Shelly Miller Moore
 
Making Beer Money from your Travel Blog
Heartwood Digital
 
طباعة التقرير - إختبر درجة إبداعك احمد الذهب
Ahmed Dahab
 
Grandmas Recipes by Wendy Pang
Wendy Pang
 
Matriz de valoracion pid y aamtic
Polo Apolo
 
Hakukoneet ja mittaristo #digisawotta
Aki Karkulahti
 
Mapa conceptual. GESTIÓN DE PROYECTO
Marcela Leon
 
Plan lector 4 todos somos iguales
Luis Miguel Galiano Velasquez
 
5 pen pc tecnology
manjushapdk
 
How can communities shape economic development and create quality jobs
Urban Habitat
 
Ad

Similar to Beginning Object-Oriented JavaScript (20)

KEY
Javascript tid-bits
David Atchley
 
PPTX
Object Oriented JavaScript
Julie Iskander
 
PPT
Advanced Javascript
relay12
 
PPT
Advanced Javascript
Manikanda kumar
 
PPTX
Ajaxworld
deannalagason
 
PDF
Js objects
anubavam-techkt
 
PPTX
Typescript barcelona
Christoffer Noring
 
PPT
Javascript Object Oriented Programming
Bunlong Van
 
PPTX
OO in JavaScript
Gunjan Kumar
 
PDF
JavaScript Survival Guide
Giordano Scalzo
 
PPTX
Javascript Objects Deep Dive
Manish Jangir
 
PPTX
WEB222-lecture-4.pptx
RohitSharma318779
 
PPTX
Object oriented javascript
Usman Mehmood
 
PPTX
JavsScript OOP
LearningTech
 
PDF
JavaScript Essentials
Triphon Statkov
 
PDF
JavaScript Core
François Sarradin
 
PDF
The many facets of code reuse in JavaScript
Leonardo Borges
 
PPTX
Understanding-Objects-in-Javascript.pptx
MariaTrinidadTumanga
 
PPTX
Ian 20150116 java script oop
LearningTech
 
PPTX
4front en
Vitaly Hornik
 
Javascript tid-bits
David Atchley
 
Object Oriented JavaScript
Julie Iskander
 
Advanced Javascript
relay12
 
Advanced Javascript
Manikanda kumar
 
Ajaxworld
deannalagason
 
Js objects
anubavam-techkt
 
Typescript barcelona
Christoffer Noring
 
Javascript Object Oriented Programming
Bunlong Van
 
OO in JavaScript
Gunjan Kumar
 
JavaScript Survival Guide
Giordano Scalzo
 
Javascript Objects Deep Dive
Manish Jangir
 
WEB222-lecture-4.pptx
RohitSharma318779
 
Object oriented javascript
Usman Mehmood
 
JavsScript OOP
LearningTech
 
JavaScript Essentials
Triphon Statkov
 
JavaScript Core
François Sarradin
 
The many facets of code reuse in JavaScript
Leonardo Borges
 
Understanding-Objects-in-Javascript.pptx
MariaTrinidadTumanga
 
Ian 20150116 java script oop
LearningTech
 
4front en
Vitaly Hornik
 
Ad

More from Stoyan Stefanov (20)

PDF
Reactive JavaScript
Stoyan Stefanov
 
PPTX
YSlow hacking
Stoyan Stefanov
 
PPTX
Liking performance
Stoyan Stefanov
 
PPTX
JavaScript Performance Patterns
Stoyan Stefanov
 
PPTX
JavaScript performance patterns
Stoyan Stefanov
 
PPTX
High Performance Social Plugins
Stoyan Stefanov
 
PDF
Social Button BFFs
Stoyan Stefanov
 
PPTX
JavaScript навсякъде
Stoyan Stefanov
 
PPTX
JavaScript is everywhere
Stoyan Stefanov
 
PDF
JavaScript shell scripting
Stoyan Stefanov
 
PDF
JavaScript for PHP developers
Stoyan Stefanov
 
PDF
WPO @ PubCon 2010
Stoyan Stefanov
 
PDF
Progressive Downloads and Rendering - take #2
Stoyan Stefanov
 
PDF
Progressive Downloads and Rendering
Stoyan Stefanov
 
PDF
Performance patterns
Stoyan Stefanov
 
PDF
Voices that matter: High Performance Web Sites
Stoyan Stefanov
 
PDF
Psychology of performance
Stoyan Stefanov
 
PPT
3-in-1 YSlow
Stoyan Stefanov
 
PDF
CSS and image optimization
Stoyan Stefanov
 
PPT
High-performance DOM scripting
Stoyan Stefanov
 
Reactive JavaScript
Stoyan Stefanov
 
YSlow hacking
Stoyan Stefanov
 
Liking performance
Stoyan Stefanov
 
JavaScript Performance Patterns
Stoyan Stefanov
 
JavaScript performance patterns
Stoyan Stefanov
 
High Performance Social Plugins
Stoyan Stefanov
 
Social Button BFFs
Stoyan Stefanov
 
JavaScript навсякъде
Stoyan Stefanov
 
JavaScript is everywhere
Stoyan Stefanov
 
JavaScript shell scripting
Stoyan Stefanov
 
JavaScript for PHP developers
Stoyan Stefanov
 
WPO @ PubCon 2010
Stoyan Stefanov
 
Progressive Downloads and Rendering - take #2
Stoyan Stefanov
 
Progressive Downloads and Rendering
Stoyan Stefanov
 
Performance patterns
Stoyan Stefanov
 
Voices that matter: High Performance Web Sites
Stoyan Stefanov
 
Psychology of performance
Stoyan Stefanov
 
3-in-1 YSlow
Stoyan Stefanov
 
CSS and image optimization
Stoyan Stefanov
 
High-performance DOM scripting
Stoyan Stefanov
 

Recently uploaded (20)

PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
Q2 Leading a Tableau User Group - Onboarding
lward7
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Q2 Leading a Tableau User Group - Onboarding
lward7
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
July Patch Tuesday
Ivanti
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 

Beginning Object-Oriented JavaScript