SlideShare a Scribd company logo
Object Oriented JavaScript
 JavaScript is a flexible                              Embrace the principles of
and expressive language                                    OO design and how
 that should be written                                prototypical languages like
 clearly and concisely.                                   JavaScript fit into this
                                                               paradigm.




                            JavaScript is one of the
                              cornerstones to the
                             powerful set of tools
                              made available by
                                   HTML5
Remember Why?

Just to name some of the reasons...
  Encapsulation
  Composition
  Inheritance
  Polymorphism
Prototype Based Language

No formal class defn.
Objects are prototypes
Inheritance through
  cloning
  ex nihilo "from nothing"
Instance Objects
BaseSoup = function() {
  name = "simple soup";
  price = 7.00;
  ingredients = ["water", "salt", "mirepoix"];
}

BaseSoup.prototype.menuDisplay = function() {
  return name.concat(" ").concat(price);
}

var soup = new BaseSoup();
soup.menuDisplay();
Composition, Private Methods...
BaseSoup = function() {
  name = "simple soup";
  priceMgr = new PriceManager();
  ingredients = ["water", "salt", "mirepoix"];
  price = function() {
     return priceMgr.price(this);
  }
}

BaseSoup.prototype.menuDisplay = function() {
  return name.concat(" ").concat(price());
}
Inheritance
CrabBisque = function() {};

//Lets inherit from the BaseSoup object from the previous slides
CrabBisque.prototype = new BaseSoup;
CrabBisque.prototype.constructor = CrabBisque;
CrabBisque.prototype.parent = BaseSoup.prototype;

CrabBisque.prototype.description = function() {
  return "Delicious crab in a rich cream broth";
}

var bisque = new CrabBisque();
bisque.menuDisplay();
Polymorphic Jackpot
CrabBisque = function() {
   name = "Crab Bisque";
   ingredients = ["salt", "mirepoix",
                       "heavy cream", "crab", "butter",
                        "leeks", "pepper", "tomato paste"];
};

CrabBisque.prototype = new BaseSoup;
CrabBisque.prototype.constructor = CrabBisque;
CrabBisque.prototype.parent = BaseSoup.prototype;

var bisque = new CrabBisque();
bisque.display();
bisque.description();
Soup? Inspiration.
call ~ super
CrabBisque = function() {
   BaseSoup.call(this); //call the super object constructor
   name = "Crab Bisque";
   ingredients = ["salt", "mirepoix",
                       "heavy cream", "crab", "butter"
                        "leeks", "pepper", "tomato paste"];
};

CrabBisque.prototype.description = function() {
  return BaseSoup.prototype.description.call(this); //call the super method
}
"From Nothing"
var lunch = {soup: new Jambalaya(), bread: true,
 drink: "Coke", burp: function() { return "yum"}};
Static Objects
SoupFactory = (function() {
  return {
     serve: function(person) {
       switch(person.name()) {
           case "Newman":
             return new Jambalaya();
           case "George":
             return new CrabBisque();
           case "Elaine":
             return new Mulligatawny();
           default:
             return new LimaBean();
       }
     }
  }

})();
Closures / Anonymous Functions
//function in a function
//retains a copy of the local variable despite being an anon function
FatCat = function() {
   var weight = 4;
   this.eat = function() { weight++; };
   this.weighIn = function() { alert(weight); };
   this.speak = function() {
      kittyTalk = function() { alert(meow); }
      //NOTE: meow is defined _after_ the anon above...it still works!
      var meow = "Meeeooww";
      return kittyTalk; //just got functional
   }
}
Functional Sprinkles of Goodness
function each(arrayOfStuff, action) {

    for(var i = 0; i < arrayOfStuff.length; i++) {
       action(arrayOfStuff[i]);
    }

}

each([1,2,3,4,5], alert);

More Related Content

Viewers also liked (6)

MongoDB Part 2
MongoDB Part 2MongoDB Part 2
MongoDB Part 2
techwhizbang
 
Let's earn some media by Jelle Kolleman, Red Urban
Let's earn some media by Jelle Kolleman, Red UrbanLet's earn some media by Jelle Kolleman, Red Urban
Let's earn some media by Jelle Kolleman, Red Urban
Hyves
 
Continuous Delivery
Continuous Delivery Continuous Delivery
Continuous Delivery
Dmitry Buzdin
 
Making Pretty Charts in Splunk
Making Pretty Charts in SplunkMaking Pretty Charts in Splunk
Making Pretty Charts in Splunk
Splunk
 
Red Urban presenteert succesvolle Hyvescampagnes & learnings
Red Urban presenteert succesvolle Hyvescampagnes & learnings Red Urban presenteert succesvolle Hyvescampagnes & learnings
Red Urban presenteert succesvolle Hyvescampagnes & learnings
Hyves
 
An ASAP Validation Implementation Approach by Qualit Consulting
An ASAP Validation Implementation Approach by  Qualit ConsultingAn ASAP Validation Implementation Approach by  Qualit Consulting
An ASAP Validation Implementation Approach by Qualit Consulting
aesww
 
Let's earn some media by Jelle Kolleman, Red Urban
Let's earn some media by Jelle Kolleman, Red UrbanLet's earn some media by Jelle Kolleman, Red Urban
Let's earn some media by Jelle Kolleman, Red Urban
Hyves
 
Continuous Delivery
Continuous Delivery Continuous Delivery
Continuous Delivery
Dmitry Buzdin
 
Making Pretty Charts in Splunk
Making Pretty Charts in SplunkMaking Pretty Charts in Splunk
Making Pretty Charts in Splunk
Splunk
 
Red Urban presenteert succesvolle Hyvescampagnes & learnings
Red Urban presenteert succesvolle Hyvescampagnes & learnings Red Urban presenteert succesvolle Hyvescampagnes & learnings
Red Urban presenteert succesvolle Hyvescampagnes & learnings
Hyves
 
An ASAP Validation Implementation Approach by Qualit Consulting
An ASAP Validation Implementation Approach by  Qualit ConsultingAn ASAP Validation Implementation Approach by  Qualit Consulting
An ASAP Validation Implementation Approach by Qualit Consulting
aesww
 

Similar to Object Oriented JavaScript (20)

Intro to Advanced JavaScript
Intro to Advanced JavaScriptIntro to Advanced JavaScript
Intro to Advanced JavaScript
ryanstout
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
David Atchley
 
Professional JavaScript Development - Creating Reusable Code
Professional JavaScript Development -  Creating Reusable CodeProfessional JavaScript Development -  Creating Reusable Code
Professional JavaScript Development - Creating Reusable Code
Wildan Maulana
 
JavaScript 1.5 to 2.0 (TomTom)
JavaScript 1.5 to 2.0 (TomTom)JavaScript 1.5 to 2.0 (TomTom)
JavaScript 1.5 to 2.0 (TomTom)
jeresig
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
Enrique Juan de Dios
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 
Js objects
Js objectsJs objects
Js objects
anubavam-techkt
 
V8
V8V8
V8
Burcu Dogan
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
Michael Girouard
 
Wakanday JS201 Best Practices
Wakanday JS201 Best PracticesWakanday JS201 Best Practices
Wakanday JS201 Best Practices
Juergen Fesslmeier
 
Orlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't SuckOrlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't Suck
erockendude
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
Julie Iskander
 
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
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
Stoyan Stefanov
 
JavaScript Core
JavaScript CoreJavaScript Core
JavaScript Core
François Sarradin
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
deannalagason
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
relay12
 
Intro to Advanced JavaScript
Intro to Advanced JavaScriptIntro to Advanced JavaScript
Intro to Advanced JavaScript
ryanstout
 
Professional JavaScript Development - Creating Reusable Code
Professional JavaScript Development -  Creating Reusable CodeProfessional JavaScript Development -  Creating Reusable Code
Professional JavaScript Development - Creating Reusable Code
Wildan Maulana
 
JavaScript 1.5 to 2.0 (TomTom)
JavaScript 1.5 to 2.0 (TomTom)JavaScript 1.5 to 2.0 (TomTom)
JavaScript 1.5 to 2.0 (TomTom)
jeresig
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 
Orlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't SuckOrlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't Suck
erockendude
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
Julie Iskander
 
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
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
relay12
 
Ad

Recently uploaded (20)

How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Ad

Object Oriented JavaScript

  • 1. Object Oriented JavaScript JavaScript is a flexible Embrace the principles of and expressive language OO design and how that should be written prototypical languages like clearly and concisely. JavaScript fit into this paradigm. JavaScript is one of the cornerstones to the powerful set of tools made available by HTML5
  • 2. Remember Why? Just to name some of the reasons... Encapsulation Composition Inheritance Polymorphism
  • 3. Prototype Based Language No formal class defn. Objects are prototypes Inheritance through cloning ex nihilo "from nothing"
  • 4. Instance Objects BaseSoup = function() { name = "simple soup"; price = 7.00; ingredients = ["water", "salt", "mirepoix"]; } BaseSoup.prototype.menuDisplay = function() { return name.concat(" ").concat(price); } var soup = new BaseSoup(); soup.menuDisplay();
  • 5. Composition, Private Methods... BaseSoup = function() { name = "simple soup"; priceMgr = new PriceManager(); ingredients = ["water", "salt", "mirepoix"]; price = function() { return priceMgr.price(this); } } BaseSoup.prototype.menuDisplay = function() { return name.concat(" ").concat(price()); }
  • 6. Inheritance CrabBisque = function() {}; //Lets inherit from the BaseSoup object from the previous slides CrabBisque.prototype = new BaseSoup; CrabBisque.prototype.constructor = CrabBisque; CrabBisque.prototype.parent = BaseSoup.prototype; CrabBisque.prototype.description = function() { return "Delicious crab in a rich cream broth"; } var bisque = new CrabBisque(); bisque.menuDisplay();
  • 7. Polymorphic Jackpot CrabBisque = function() { name = "Crab Bisque"; ingredients = ["salt", "mirepoix", "heavy cream", "crab", "butter", "leeks", "pepper", "tomato paste"]; }; CrabBisque.prototype = new BaseSoup; CrabBisque.prototype.constructor = CrabBisque; CrabBisque.prototype.parent = BaseSoup.prototype; var bisque = new CrabBisque(); bisque.display(); bisque.description();
  • 9. call ~ super CrabBisque = function() { BaseSoup.call(this); //call the super object constructor name = "Crab Bisque"; ingredients = ["salt", "mirepoix", "heavy cream", "crab", "butter" "leeks", "pepper", "tomato paste"]; }; CrabBisque.prototype.description = function() { return BaseSoup.prototype.description.call(this); //call the super method }
  • 10. "From Nothing" var lunch = {soup: new Jambalaya(), bread: true, drink: "Coke", burp: function() { return "yum"}};
  • 11. Static Objects SoupFactory = (function() { return { serve: function(person) { switch(person.name()) { case "Newman": return new Jambalaya(); case "George": return new CrabBisque(); case "Elaine": return new Mulligatawny(); default: return new LimaBean(); } } } })();
  • 12. Closures / Anonymous Functions //function in a function //retains a copy of the local variable despite being an anon function FatCat = function() { var weight = 4; this.eat = function() { weight++; }; this.weighIn = function() { alert(weight); }; this.speak = function() { kittyTalk = function() { alert(meow); } //NOTE: meow is defined _after_ the anon above...it still works! var meow = "Meeeooww"; return kittyTalk; //just got functional } }
  • 13. Functional Sprinkles of Goodness function each(arrayOfStuff, action) { for(var i = 0; i < arrayOfStuff.length; i++) { action(arrayOfStuff[i]); } } each([1,2,3,4,5], alert);