SlideShare une entreprise Scribd logo
1  sur  33
Télécharger pour lire hors ligne
Class.js (a.k.a Class.extend)
The amazing 25 line library you probably don't need
Background
What is it?
• It’s a small JavaScript library
• It was written by John Resig,
creator of jQuery, for his book
"Secrets of the JavaScript
Ninja"
• It provides a simplified API for
Class-like inheritance in
JavaScript
• Can be found at John’s site or
on NPM
Why is it interesting?
• It’s only 25 lines long (without
comments)
• It baffled me
• It’s very cleverly written
• It’s a bit of an anti-pattern
/* Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 */
// Inspired by base2 and Prototype
(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /b_superb/ : /.*/;
 
  // The base Class implementation (does nothing)
  this.Class = function(){};
 
  // Create a new Class that inherits from this class
  Class.extend = function(prop) {
    var _super = this.prototype;
   
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
   
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
           
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
           
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);        
            this._super = tmp;
           
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
   
    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
   
    // Populate our constructed prototype object
    Class.prototype = prototype;
   
    // Enforce the constructor to be what we expect
    Class.prototype.constructor = Class;
 
    // And make this class extendable
    Class.extend = arguments.callee;
   
    return Class;
  };
})();
Got Learnt
Class.js baffled me; I pored over
it until I understood it.
Then I wrote a little about it…
Actually, I wrote a lot
• Close examination of the code
• Yielded a 3700 word article
• My brain tingled with knowledge
Deep Extra Medium dive into Class.js
Usage
• Create an object to represent your prototype
• Pass the object to Class.extend to get a constructor for your new class
• The “extend” method is attached to the new constructor
• Pass a new partial prototype to constructor’s “extend” method to extend the
class
Create a class
var Pirate = Class.extend({
" init: function(isEyepatch) {
" " this.eyepatch = isEyepatch;
" },
" eyepatch: false,
" attack: function() {
" " alert("yarrrr!");
" }
});
Extend a class
var SpacePirate = Pirate.extend({
" attack: function() {
" " this._super();
" " alert("zap!");
" }
});
Use it
var blackBeard = new Pirate,
" starLord = new SpacePirate;
"
blackBeard instanceof Pirate; // true!
starLord instanceof SpacePirate; // too true!
starLord instanceof Pirate; // also true!
starLord instanceof Class; // it's all true!
blackBeard.attack(); // yarrrr!
starLord.attack(); // yarrr! zap!
Magic
Wrapped in a self-executing function expression
(function(){
// the magic goes in here
})();
Initializing, and the function test
var initializing = false,
fnTest = /xyz/.test(function(){xyz;}) ? /b_superb/ : /.*/;
The base constructor does nothing but show up in
your browser console
// The base Class implementation (does nothing)
this.Class = function(){};
Define the “extend” method “statically”
// Create a new Class that inherits from this class
Class.extend = function(prop) {/*...*/}
Get a reference to the parent prototype, create an
instance to serve as a new prototype
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;
Copy the property definition into the prototype
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
/* ... super things happen here */ :
prop[name];
}
If prop[name] is a function containing “_super”...make
“_super” a reference to the parent method
(function(name, fn){
return function() {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name])
One more time, in slow motion
• If the property to be copied is a function…
• And contains the text “_super”…
• Create a closure which returns a function…
• Which caches anything called “_super” temporarily…
• Then stores a reference to the parent prototype’s function of the same name…
• Then calls the original (not _super) function…
• Uncaching the local copy of “_super”…
• And return the results of the function
Create a new constructor which calls “init”
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if ( !initializing && this.init )
this.init.apply(this, arguments);
}
Assign the augmented prototype to the constructor
// Populate our constructed prototype object
Class.prototype = prototype;
// Enforce the constructor to be what we expect
Class.prototype.constructor = Class;
Attach “extend” to the new constructor, and return
the new class
// And make this class extendable
Class.extend = arguments.callee;
return Class;
Demo
Your probably don’t
need it
Remember: JavaScript ain’t Java
You can create “types” in JavaScript with
constructors and instances
But JavaScript types do not have a guaranteed interface.
• All objects can be modified regardless of type (the constructor)
• Object API can change at any time addition or deletion of properties
• Types have no guaranteed interface, only a prototype chain
• You have to test the api of unknown objects
In JavaScript…
Idiomatic JavaScript
Work with JavaScript in a JavaScripty way.
Remember
• Creating an object in JavaScript is trivial
• Copying objects in JavaScript is trivial
• In most cases, most objects in JavaScript are singletons
• Singletons can "inherit" by using a mix-in pattern
• _.assign/extend
• jQuery.extend
{}
for (x in y) {
" z[x] = y[x];
}
When do you need Class.js?
• When you must define multiple types of an object which…
• Share a common, inherited API, which…
• Needs access to the parent API
Examples…
• User management in an application with many types of users (employees,
managers, space pirates)
• Product listing with a base "product" definition inherited product types where
all products share common features / API
Alternatives…
• Object.create and Object.getPrototypeOf
• Copy Module pattern
• Mix-ins
Resources
• http://ejohn.org/blog/simple-javascript-inheritance/
• http://abouthalf.com/development/understanding-class-js/
• https://www.npmjs.org/package/class.extend
• https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/
Details_of_the_Object_Model
• http://ejohn.org/blog/objectgetprototypeof/
• https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/
getPrototypeOf
• https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/
create

Contenu connexe

Tendances

JRuby in Java Projects
JRuby in Java ProjectsJRuby in Java Projects
JRuby in Java Projectsjazzman1980
 
Scalable JavaScript Application Architecture
Scalable JavaScript Application ArchitectureScalable JavaScript Application Architecture
Scalable JavaScript Application ArchitectureNicholas Zakas
 
Node.JS error handling best practices
Node.JS error handling best practicesNode.JS error handling best practices
Node.JS error handling best practicesYoni Goldberg
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptzand3rs
 
Agile JavaScript Testing
Agile JavaScript TestingAgile JavaScript Testing
Agile JavaScript TestingScott Becker
 
JS Frameworks Day April,26 of 2014
JS Frameworks Day April,26 of 2014JS Frameworks Day April,26 of 2014
JS Frameworks Day April,26 of 2014DA-14
 
Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)Anna Khabibullina
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design PatternsAddy Osmani
 
Testable JavaScript: Application Architecture
Testable JavaScript:  Application ArchitectureTestable JavaScript:  Application Architecture
Testable JavaScript: Application ArchitectureMark Trostler
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyNick Sieger
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptForziatech
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiRan Mizrahi
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipseanshunjain
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScriptYakov Fain
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript BasicsMindfire Solutions
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchMats Bryntse
 
Survive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and TricksSurvive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and TricksJuho Vepsäläinen
 
Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6Devang Garach
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module PatternsNicholas Jansma
 

Tendances (20)

JRuby in Java Projects
JRuby in Java ProjectsJRuby in Java Projects
JRuby in Java Projects
 
Scalable JavaScript Application Architecture
Scalable JavaScript Application ArchitectureScalable JavaScript Application Architecture
Scalable JavaScript Application Architecture
 
Node.JS error handling best practices
Node.JS error handling best practicesNode.JS error handling best practices
Node.JS error handling best practices
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
Agile JavaScript Testing
Agile JavaScript TestingAgile JavaScript Testing
Agile JavaScript Testing
 
JS Frameworks Day April,26 of 2014
JS Frameworks Day April,26 of 2014JS Frameworks Day April,26 of 2014
JS Frameworks Day April,26 of 2014
 
Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design Patterns
 
JavaScript Patterns
JavaScript PatternsJavaScript Patterns
JavaScript Patterns
 
Testable JavaScript: Application Architecture
Testable JavaScript:  Application ArchitectureTestable JavaScript:  Application Architecture
Testable JavaScript: Application Architecture
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
 
Survive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and TricksSurvive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and Tricks
 
Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module Patterns
 

En vedette

Reader's Advisory: Science Fiction
Reader's Advisory: Science FictionReader's Advisory: Science Fiction
Reader's Advisory: Science Fictionhcplharriet
 
Atlanta mobile app development companies
Atlanta mobile app development companiesAtlanta mobile app development companies
Atlanta mobile app development companieswilliam_jones
 
Sci fi (media-story)
Sci fi (media-story)Sci fi (media-story)
Sci fi (media-story)LewisPashley
 
LivePolicy Briefings Service & Prices May 2015
LivePolicy Briefings Service & Prices May 2015LivePolicy Briefings Service & Prices May 2015
LivePolicy Briefings Service & Prices May 2015Chris Hayes
 
Chrystos, a Native American LGBT leader
Chrystos, a Native American LGBT leaderChrystos, a Native American LGBT leader
Chrystos, a Native American LGBT leaderKumiki Arata
 
Roman Valchuk "Introducing to DevOps technologies"
Roman Valchuk "Introducing to DevOps technologies"Roman Valchuk "Introducing to DevOps technologies"
Roman Valchuk "Introducing to DevOps technologies"Vadym Muliavka
 
ทฤษฎีการเรียนรู้ของธอร์นไดค์
ทฤษฎีการเรียนรู้ของธอร์นไดค์ทฤษฎีการเรียนรู้ของธอร์นไดค์
ทฤษฎีการเรียนรู้ของธอร์นไดค์mekshak
 

En vedette (11)

Resume
ResumeResume
Resume
 
Reader's Advisory: Science Fiction
Reader's Advisory: Science FictionReader's Advisory: Science Fiction
Reader's Advisory: Science Fiction
 
Atlanta mobile app development companies
Atlanta mobile app development companiesAtlanta mobile app development companies
Atlanta mobile app development companies
 
Sci fi (media-story)
Sci fi (media-story)Sci fi (media-story)
Sci fi (media-story)
 
LivePolicy Briefings Service & Prices May 2015
LivePolicy Briefings Service & Prices May 2015LivePolicy Briefings Service & Prices May 2015
LivePolicy Briefings Service & Prices May 2015
 
Recursos naturales
Recursos naturalesRecursos naturales
Recursos naturales
 
Stop a la flaccidez: nota de prensa
Stop a la flaccidez: nota de prensaStop a la flaccidez: nota de prensa
Stop a la flaccidez: nota de prensa
 
Chrystos, a Native American LGBT leader
Chrystos, a Native American LGBT leaderChrystos, a Native American LGBT leader
Chrystos, a Native American LGBT leader
 
Roman Valchuk "Introducing to DevOps technologies"
Roman Valchuk "Introducing to DevOps technologies"Roman Valchuk "Introducing to DevOps technologies"
Roman Valchuk "Introducing to DevOps technologies"
 
Cuadro informativo
Cuadro informativo Cuadro informativo
Cuadro informativo
 
ทฤษฎีการเรียนรู้ของธอร์นไดค์
ทฤษฎีการเรียนรู้ของธอร์นไดค์ทฤษฎีการเรียนรู้ของธอร์นไดค์
ทฤษฎีการเรียนรู้ของธอร์นไดค์
 

Similaire à ClassJS

java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructorShivam Singhal
 
Constructors in Java (2).pdf
Constructors in Java (2).pdfConstructors in Java (2).pdf
Constructors in Java (2).pdfkumari36
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery PluginRavi Mone
 
13 java beans
13 java beans13 java beans
13 java beanssnopteck
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)Prof. Erwin Globio
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQueryBobby Bryant
 
JavaScript Coding with Class
JavaScript Coding with ClassJavaScript Coding with Class
JavaScript Coding with Classdavidwalsh83
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya Johnny
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming ConceptsBhushan Nagaraj
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design PatternsPham Huy Tung
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Soumen Santra
 

Similaire à ClassJS (20)

java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 
Java Constructor
Java ConstructorJava Constructor
Java Constructor
 
Constructors in Java (2).pdf
Constructors in Java (2).pdfConstructors in Java (2).pdf
Constructors in Java (2).pdf
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
 
13 java beans
13 java beans13 java beans
13 java beans
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
JavaScript Coding with Class
JavaScript Coding with ClassJavaScript Coding with Class
JavaScript Coding with Class
 
oop 3.pptx
oop 3.pptxoop 3.pptx
oop 3.pptx
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design Patterns
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
 

ClassJS

  • 1. Class.js (a.k.a Class.extend) The amazing 25 line library you probably don't need
  • 3. What is it? • It’s a small JavaScript library • It was written by John Resig, creator of jQuery, for his book "Secrets of the JavaScript Ninja" • It provides a simplified API for Class-like inheritance in JavaScript • Can be found at John’s site or on NPM
  • 4. Why is it interesting? • It’s only 25 lines long (without comments) • It baffled me • It’s very cleverly written • It’s a bit of an anti-pattern /* Simple JavaScript Inheritance  * By John Resig http://ejohn.org/  * MIT Licensed.  */ // Inspired by base2 and Prototype (function(){   var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /b_superb/ : /.*/;     // The base Class implementation (does nothing)   this.Class = function(){};     // Create a new Class that inherits from this class   Class.extend = function(prop) {     var _super = this.prototype;         // Instantiate a base class (but only create the instance,     // don't run the init constructor)     initializing = true;     var prototype = new this();     initializing = false;         // Copy the properties over onto the new prototype     for (var name in prop) {       // Check if we're overwriting an existing function       prototype[name] = typeof prop[name] == "function" &&         typeof _super[name] == "function" && fnTest.test(prop[name]) ?         (function(name, fn){           return function() {             var tmp = this._super;                         // Add a new ._super() method that is the same method             // but on the super-class             this._super = _super[name];                         // The method only need to be bound temporarily, so we             // remove it when we're done executing             var ret = fn.apply(this, arguments);                     this._super = tmp;                         return ret;           };         })(name, prop[name]) :         prop[name];     }         // The dummy class constructor     function Class() {       // All construction is actually done in the init method       if ( !initializing && this.init )         this.init.apply(this, arguments);     }         // Populate our constructed prototype object     Class.prototype = prototype;         // Enforce the constructor to be what we expect     Class.prototype.constructor = Class;       // And make this class extendable     Class.extend = arguments.callee;         return Class;   }; })();
  • 5. Got Learnt Class.js baffled me; I pored over it until I understood it. Then I wrote a little about it…
  • 6. Actually, I wrote a lot • Close examination of the code • Yielded a 3700 word article • My brain tingled with knowledge
  • 7. Deep Extra Medium dive into Class.js
  • 8. Usage • Create an object to represent your prototype • Pass the object to Class.extend to get a constructor for your new class • The “extend” method is attached to the new constructor • Pass a new partial prototype to constructor’s “extend” method to extend the class
  • 9. Create a class var Pirate = Class.extend({ " init: function(isEyepatch) { " " this.eyepatch = isEyepatch; " }, " eyepatch: false, " attack: function() { " " alert("yarrrr!"); " } });
  • 10. Extend a class var SpacePirate = Pirate.extend({ " attack: function() { " " this._super(); " " alert("zap!"); " } });
  • 11. Use it var blackBeard = new Pirate, " starLord = new SpacePirate; " blackBeard instanceof Pirate; // true! starLord instanceof SpacePirate; // too true! starLord instanceof Pirate; // also true! starLord instanceof Class; // it's all true! blackBeard.attack(); // yarrrr! starLord.attack(); // yarrr! zap!
  • 12. Magic
  • 13. Wrapped in a self-executing function expression (function(){ // the magic goes in here })();
  • 14. Initializing, and the function test var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /b_superb/ : /.*/;
  • 15. The base constructor does nothing but show up in your browser console // The base Class implementation (does nothing) this.Class = function(){};
  • 16. Define the “extend” method “statically” // Create a new Class that inherits from this class Class.extend = function(prop) {/*...*/}
  • 17. Get a reference to the parent prototype, create an instance to serve as a new prototype var _super = this.prototype; // Instantiate a base class (but only create the instance, // don't run the init constructor) initializing = true; var prototype = new this(); initializing = false;
  • 18. Copy the property definition into the prototype // Copy the properties over onto the new prototype for (var name in prop) { // Check if we're overwriting an existing function prototype[name] = typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name]) ? /* ... super things happen here */ : prop[name]; }
  • 19. If prop[name] is a function containing “_super”...make “_super” a reference to the parent method (function(name, fn){ return function() { var tmp = this._super; // Add a new ._super() method that is the same method // but on the super-class this._super = _super[name]; // The method only need to be bound temporarily, so we // remove it when we're done executing var ret = fn.apply(this, arguments); this._super = tmp; return ret; }; })(name, prop[name])
  • 20. One more time, in slow motion • If the property to be copied is a function… • And contains the text “_super”… • Create a closure which returns a function… • Which caches anything called “_super” temporarily… • Then stores a reference to the parent prototype’s function of the same name… • Then calls the original (not _super) function… • Uncaching the local copy of “_super”… • And return the results of the function
  • 21. Create a new constructor which calls “init” // The dummy class constructor function Class() { // All construction is actually done in the init method if ( !initializing && this.init ) this.init.apply(this, arguments); }
  • 22. Assign the augmented prototype to the constructor // Populate our constructed prototype object Class.prototype = prototype; // Enforce the constructor to be what we expect Class.prototype.constructor = Class;
  • 23. Attach “extend” to the new constructor, and return the new class // And make this class extendable Class.extend = arguments.callee; return Class;
  • 24. Demo
  • 25. Your probably don’t need it Remember: JavaScript ain’t Java
  • 26. You can create “types” in JavaScript with constructors and instances But JavaScript types do not have a guaranteed interface.
  • 27. • All objects can be modified regardless of type (the constructor) • Object API can change at any time addition or deletion of properties • Types have no guaranteed interface, only a prototype chain • You have to test the api of unknown objects In JavaScript…
  • 28. Idiomatic JavaScript Work with JavaScript in a JavaScripty way.
  • 29. Remember • Creating an object in JavaScript is trivial • Copying objects in JavaScript is trivial • In most cases, most objects in JavaScript are singletons • Singletons can "inherit" by using a mix-in pattern • _.assign/extend • jQuery.extend {} for (x in y) { " z[x] = y[x]; }
  • 30. When do you need Class.js? • When you must define multiple types of an object which… • Share a common, inherited API, which… • Needs access to the parent API
  • 31. Examples… • User management in an application with many types of users (employees, managers, space pirates) • Product listing with a base "product" definition inherited product types where all products share common features / API
  • 32. Alternatives… • Object.create and Object.getPrototypeOf • Copy Module pattern • Mix-ins
  • 33. Resources • http://ejohn.org/blog/simple-javascript-inheritance/ • http://abouthalf.com/development/understanding-class-js/ • https://www.npmjs.org/package/class.extend • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/ Details_of_the_Object_Model • http://ejohn.org/blog/objectgetprototypeof/ • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/ getPrototypeOf • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/ create