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
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 

Beginning Object-Oriented JavaScript