SlideShare una empresa de Scribd logo
1 de 44
ECMA5 approach to writing JavaScript
frameworks
Anzor Bashkhaz
@anzor_b
Isaac Gordezky
@igordezky
ECMA5
What is ECMA5?
• ECMAScript = language of JavaScript
• Final draft for v5 presented in April 2009
• Object creation/manipulation
• Supported in all major platforms
http://kangax.github.io/es5-compat-table/
API design
What is an API?
• API exposes ways to use a library/framework
• Ex: underscore
• Users of API can’t see implementation
• aka. Public API
API: Real world example
Dissecting the JavaScript Object:
Type and Prototype
5
Type
What is a type?
• Instance factory
• JavaScript ‘Object’ is a type
• Made up of:
• Constructor (returns new instance)
• Static methods (create, call,…)
• Prototype
Type
What is a type?
• Instance factory
• JavaScript ‘Object’ is a type
• Made up of:
• Constructor (returns new instance)
• Static methods (create, call,…)
• Prototype
Prototype
What is a prototype?
• Prototype = definition for new instances
• = Public API
• Object.prototype
Prototype
What is a prototype?
• Prototype = definition for new instances
• = Public API
• Object.prototype
Inside an instance
Ex: Dissecting the instance of Object
var foo = {
hello : function(){
return “hello world”;
}
};
console.log(foo);
Inside an instance
Ex: Dissecting the instance of Object
var foo = {
hello : function(){
return “hello world”;
}
};
console.log(foo);
Inside an instance
Ex: Dissecting the instance of Object
var foo = {
hello : function(){
return “hello world”;
}
};
console.log(foo);
What is __proto__?
__proto__
“Special” property on every JavaScript Object.
• foo was created from Object.
var foo = {}
• __proto__ of foo points at prototype of Object.
• Object.create(definition)
• Returns object (instance) with __proto__ pointing at
definition
• Returned object can be Object.create’d again and again
__proto__
Ex: Prototype chain and simple inheritance
var foo = {};
var bar = Object.create(foo);
__proto__
Ex: Prototype chain and simple inheritance
var foo = {};
var bar = Object.create(foo);
bar inherits methods from foo, while foo inherits methods from Object.
16
Demo
Custom Types
Ex: create type with name “type<Shape>”
var Shape = {};
Shape.constructor = { name: “type<Shape>" };
• Makes it easier to read in console versus “Object”
Custom Types
Ex: Add a prototype so instance are called “Shape”
• Add a prototype property
Shape.prototype = {};
• Set prototype.constructor.name to “Shape”
Shape.prototype.constructor = { name: “Shape” };
Custom Types – constructor
Ex: Add a constructor that returns instance of Shape
• Object.create() creates a new unique instance of Shape
Shape.create = function(){
return Object.create(Shape.prototype);
};
var shape1 = Shape.create();
var shape2 = Shape.create();
shape1.color = “blue”;
Custom Types – constructor
Ex: Add a constructor that returns instance of Shape
• Object.create() creates a new unique instance of Shape
Shape.create = function(){
return Object.create(Shape.prototype);
};
var shape1 = Shape.create();
var shape2 = Shape.create();
shape1.color = “blue”;
Properties
21
Defining Properties
• Good API design = preventing unintended use
• Read-only properties
Object.defineProperty(object, propertyName, descriptor)
• Descriptor
• Enumerable
• Configurable
• Writable
• Value
Read more at MDN (http://mzl.la/1cCtwPN)
Defining Properties
• Enumerable
true if the property is present while iterating through the object. (for prop in obj).
Defaults to false.
• Configurable
true if the property descriptor can be modified or property deleted on object.
Defaults to false.
• Writable
true if the property’s value can be changed? ( radius= 20)
Defaults to false.
• Value
Value of the property
Defining Properties
var Shape = {};
Object.defineProperty(Shape, “constructor”,
{
value: { name: “type<Shape>” }
});
Object.defineProperty(Shape, “prototype”,
{
value: {}
});
Object.defineProperty(Shape.prototype, “constructor”,
{
value: { name: “Shape” }
});
Defining Properties
Original (assignment)
ECMA5
(Object.defineProperty)
Read-only properties guard the API from unintended use.
Public vs. Private
26
Private
Ex: Add a private area to instances of Shape
Shape.create = function() {
var privateArea = {
public : Object.create(Shape.prototype),
secret : 5,
}
return privateArea.public;
};
var shape1 = Shape.create();
shape1.color = “blue”;
Private
Ex: Add methods to inspect the private area
Shape.create = function() {
var privateArea = {
public : Object.create(Shape.prototype),
secret : Math.floor((Math.random() * 10) + 1), //random secret per instance
privateInspect : function() {
console.log(this); //this points at instance
}
};
//public inspect function
privateArea.public.inspect = function() {
return privateArea.privateInspect();
};
return privateArea.public;
};
Private
Result:
Getters and Setters
30
Getters and Setters
• Important concept in framework design (especially UI)
• Backbone.js uses “set()” method
Ex: model.set(propertyName, value);
> “change” event triggered
> Views update their UI
• Ideally we want:
shape1.color = “blue”;
> This should trigger a color change (using DOM/canvas)
• Possible with Object.defineProperty
ECMA5 way to define setters/getters
Getters and Setters
Ex: Define setter for color to notify of change
var circle = {},
var currentValue;
Object.defineProperty(circle, “color”,
{
get: function(){
return currentValue;
},
set : function(value){
currentValue = value;
console.log(“color changed to “ + value);
}
});
Getters and Setters
Ex: Define setter for color to notify of change
var circle = {},
var currentValue;
Object.defineProperty(circle, “color”,
{
get: function(){
return currentValue;
},
set : function(value){
currentValue = value;
console.log(“color changed to “ + value);
}
});
Getters and Setters
Ex: Helper function for defining properties
var defineProperty = function(object, propertyName) {
Object.defineProperty(object, propertyName, {
get : function() {
return object[propertyName];
},
set : function(value) {
object[propertyName] = value;
console.log(propertyName + "changed to " + value);
}
});
};
var circle2 = {};
defineProperty(circle2, "color");
Beyond Private
Covered so far:
Public
• Leverages inheritance pattern
• Final instance has access to all methods in __proto__ chain
Private
• Instance specific data unexposed to the outside
What about inheriting non-public methods?
Protected to the rescue!
Protected
• Leverages inheritance pattern
• Allows child types to re-implement methods from their parents.
• Provides non-public APIs to child types.
The extend! Requirejs plugin
36
extend!
• Declarative syntax for public, private and protected
functions
• hooks for init and destroy
• Public values are defined as:
• Property getters/setters on public
• Property stored in privateArea
• Non-functions in public create notifiable properties
Ex: size triggers sizeChanged()
• All methods have ‘this’ pointing to the privateArea
• Uses require.js’ plugin system to hide parser
Case Study: A Simple Class System
38
Shapes
Objective: Build an API to draw shapes using HTML5 canvas.
type<Shape>
• Manage canvas and DOM
• Properties: color
• Protected interface: resize
• Abstract interface: draw
type<Circle> extends type<Shape>
• Implements draw interface
• Properties: radius
• Leverages resize interface
Shape.color: a property
Shape.color (Public)
• Property available on any shape object
Shape.colorChanged (Private)
• Automatically triggered when the color
property is changed
• Triggers a re-draw
Shape.color (Private)
• Stores the value of color in Shape’s privateArea
• Allows Shape’s methods to access color
without the setter/getter
Shape.resize: a protected interface
Size
• Public read-only property of Shape
• Stored in the privateArea of Shape
Resize
• Exposed to child types through protected
• Sets size in the privateArea of Shape
• Resizes DOM elements
Radius
• Public property of Circle
• Triggers protected Shape.resize
Shape.draw: a virtual method
Shape.draw (Public)
• Public draw API for all Shapes
• Calls virtual draw with canvas context
from Shape’s privateArea
Shape.draw (Protected)
• Abstract protected draw function
must be overridden by child classes
Circle.draw (Protected)
• Overrides Shape.draw (Protected)
• Doesn’t know or care where context
came from
Demo
43
Thank you
Anzor Bashkhaz
@anzor_b
Isaac Gordezky
@igordezky

Más contenido relacionado

La actualidad más candente

오브젝트C(pdf)
오브젝트C(pdf)오브젝트C(pdf)
오브젝트C(pdf)sunwooindia
 
Irving iOS Jumpstart Meetup - Objective-C Session 2
Irving iOS Jumpstart Meetup - Objective-C Session 2Irving iOS Jumpstart Meetup - Objective-C Session 2
Irving iOS Jumpstart Meetup - Objective-C Session 2irving-ios-jumpstart
 
Freebase and the iPhone
Freebase and the iPhoneFreebase and the iPhone
Freebase and the iPhoneAlec Flett
 
Artdm170 Week5 Intro To Flash
Artdm170 Week5 Intro To FlashArtdm170 Week5 Intro To Flash
Artdm170 Week5 Intro To FlashGilbert Guerrero
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)allanh0526
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - ObjectsWebStackAcademy
 
Android webinar class_4
Android webinar class_4Android webinar class_4
Android webinar class_4Edureka!
 
Type level programming in Scala
Type level programming in ScalaType level programming in Scala
Type level programming in ScalaIkhoon Eom
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
Prototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScriptPrototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScriptSunny Sharma
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modelingCodemotion
 
C# Starter L02-Classes and Objects
C# Starter L02-Classes and ObjectsC# Starter L02-Classes and Objects
C# Starter L02-Classes and ObjectsMohammad Shaker
 
Scala for scripting
Scala for scriptingScala for scripting
Scala for scriptingmichid
 
TDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with AndroidTDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with AndroidCodemotion
 
Functions and Objects in JavaScript
Functions and Objects in JavaScript Functions and Objects in JavaScript
Functions and Objects in JavaScript Dhananjay Kumar
 

La actualidad más candente (20)

Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
오브젝트C(pdf)
오브젝트C(pdf)오브젝트C(pdf)
오브젝트C(pdf)
 
Scala. Inception.
Scala. Inception.Scala. Inception.
Scala. Inception.
 
Irving iOS Jumpstart Meetup - Objective-C Session 2
Irving iOS Jumpstart Meetup - Objective-C Session 2Irving iOS Jumpstart Meetup - Objective-C Session 2
Irving iOS Jumpstart Meetup - Objective-C Session 2
 
Freebase and the iPhone
Freebase and the iPhoneFreebase and the iPhone
Freebase and the iPhone
 
Refactoring
RefactoringRefactoring
Refactoring
 
Artdm170 Week5 Intro To Flash
Artdm170 Week5 Intro To FlashArtdm170 Week5 Intro To Flash
Artdm170 Week5 Intro To Flash
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
Quick swift tour
Quick swift tourQuick swift tour
Quick swift tour
 
Android webinar class_4
Android webinar class_4Android webinar class_4
Android webinar class_4
 
Type level programming in Scala
Type level programming in ScalaType level programming in Scala
Type level programming in Scala
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Prototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScriptPrototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScript
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
C# Starter L02-Classes and Objects
C# Starter L02-Classes and ObjectsC# Starter L02-Classes and Objects
C# Starter L02-Classes and Objects
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
Scala for scripting
Scala for scriptingScala for scripting
Scala for scripting
 
TDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with AndroidTDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with Android
 
Functions and Objects in JavaScript
Functions and Objects in JavaScript Functions and Objects in JavaScript
Functions and Objects in JavaScript
 

Destacado

Javascript Framework Acessibiliity Review
Javascript Framework Acessibiliity ReviewJavascript Framework Acessibiliity Review
Javascript Framework Acessibiliity ReviewAimee Maree Forsstrom
 
Offline Mode - Web Applications Offline
Offline Mode - Web Applications OfflineOffline Mode - Web Applications Offline
Offline Mode - Web Applications OfflinePatrick Leckey
 
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-FrameworksZepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-FrameworksThomas Fuchs
 
Metaprogramming JavaScript
Metaprogramming  JavaScriptMetaprogramming  JavaScript
Metaprogramming JavaScriptdanwrong
 
Paweł Danielewski, Jak wygenerować 56% konwersji na Landing Page i 193.000 zł...
Paweł Danielewski, Jak wygenerować 56% konwersji na Landing Page i 193.000 zł...Paweł Danielewski, Jak wygenerować 56% konwersji na Landing Page i 193.000 zł...
Paweł Danielewski, Jak wygenerować 56% konwersji na Landing Page i 193.000 zł...Sprawny Marketing by MaxROY.com
 
Creating responsive landing pages using LeadSquared
Creating responsive landing pages using LeadSquaredCreating responsive landing pages using LeadSquared
Creating responsive landing pages using LeadSquaredLeadSquared
 
WebApps e Frameworks Javascript
WebApps e Frameworks JavascriptWebApps e Frameworks Javascript
WebApps e Frameworks Javascriptmeet2Brains
 
50 Landing Page Best Practices
50 Landing Page Best Practices50 Landing Page Best Practices
50 Landing Page Best Practicesion interactive
 
Unobtrusive JavaScript with jQuery
Unobtrusive JavaScript with jQueryUnobtrusive JavaScript with jQuery
Unobtrusive JavaScript with jQuerySimon Willison
 
Writing Efficient JavaScript
Writing Efficient JavaScriptWriting Efficient JavaScript
Writing Efficient JavaScriptNicholas Zakas
 

Destacado (11)

Javascript Framework Acessibiliity Review
Javascript Framework Acessibiliity ReviewJavascript Framework Acessibiliity Review
Javascript Framework Acessibiliity Review
 
Offline Mode - Web Applications Offline
Offline Mode - Web Applications OfflineOffline Mode - Web Applications Offline
Offline Mode - Web Applications Offline
 
Create a landing page
Create a landing pageCreate a landing page
Create a landing page
 
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-FrameworksZepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
 
Metaprogramming JavaScript
Metaprogramming  JavaScriptMetaprogramming  JavaScript
Metaprogramming JavaScript
 
Paweł Danielewski, Jak wygenerować 56% konwersji na Landing Page i 193.000 zł...
Paweł Danielewski, Jak wygenerować 56% konwersji na Landing Page i 193.000 zł...Paweł Danielewski, Jak wygenerować 56% konwersji na Landing Page i 193.000 zł...
Paweł Danielewski, Jak wygenerować 56% konwersji na Landing Page i 193.000 zł...
 
Creating responsive landing pages using LeadSquared
Creating responsive landing pages using LeadSquaredCreating responsive landing pages using LeadSquared
Creating responsive landing pages using LeadSquared
 
WebApps e Frameworks Javascript
WebApps e Frameworks JavascriptWebApps e Frameworks Javascript
WebApps e Frameworks Javascript
 
50 Landing Page Best Practices
50 Landing Page Best Practices50 Landing Page Best Practices
50 Landing Page Best Practices
 
Unobtrusive JavaScript with jQuery
Unobtrusive JavaScript with jQueryUnobtrusive JavaScript with jQuery
Unobtrusive JavaScript with jQuery
 
Writing Efficient JavaScript
Writing Efficient JavaScriptWriting Efficient JavaScript
Writing Efficient JavaScript
 

Similar a ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz

JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)Eduard Tomàs
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016Codemotion
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design PatternsPham Huy Tung
 
Java EE Revisits Design Patterns
Java EE Revisits Design PatternsJava EE Revisits Design Patterns
Java EE Revisits Design PatternsAlex Theedom
 
Java EE revisits design patterns
Java EE revisits design patternsJava EE revisits design patterns
Java EE revisits design patternsAlex Theedom
 
Java EE revisits design patterns
Java EE revisits design patterns Java EE revisits design patterns
Java EE revisits design patterns Alex Theedom
 
SE2016 - Java EE revisits design patterns 2016
SE2016 - Java EE revisits design patterns 2016SE2016 - Java EE revisits design patterns 2016
SE2016 - Java EE revisits design patterns 2016Alex Theedom
 
JSLounge - TypeScript 소개
JSLounge - TypeScript 소개JSLounge - TypeScript 소개
JSLounge - TypeScript 소개Reagan Hwang
 
Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworksTomáš Kypta
 
Customizing the Presentation Model and Physical Renderer in Siebel Open UI
Customizing the Presentation Model and Physical Renderer in Siebel Open UICustomizing the Presentation Model and Physical Renderer in Siebel Open UI
Customizing the Presentation Model and Physical Renderer in Siebel Open UITech OneStop
 
Javascript patterns
Javascript patternsJavascript patterns
Javascript patternsthinkphp
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptzand3rs
 

Similar a ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz (20)

25csharp
25csharp25csharp
25csharp
 
25c
25c25c
25c
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Introduction to java and oop
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design Patterns
 
Java EE Revisits Design Patterns
Java EE Revisits Design PatternsJava EE Revisits Design Patterns
Java EE Revisits Design Patterns
 
Java EE revisits design patterns
Java EE revisits design patternsJava EE revisits design patterns
Java EE revisits design patterns
 
JS Essence
JS EssenceJS Essence
JS Essence
 
Java EE revisits design patterns
Java EE revisits design patterns Java EE revisits design patterns
Java EE revisits design patterns
 
SE2016 - Java EE revisits design patterns 2016
SE2016 - Java EE revisits design patterns 2016SE2016 - Java EE revisits design patterns 2016
SE2016 - Java EE revisits design patterns 2016
 
Angular
AngularAngular
Angular
 
Advance JS and oop
Advance JS and oopAdvance JS and oop
Advance JS and oop
 
Oops
OopsOops
Oops
 
JSLounge - TypeScript 소개
JSLounge - TypeScript 소개JSLounge - TypeScript 소개
JSLounge - TypeScript 소개
 
Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworks
 
Customizing the Presentation Model and Physical Renderer in Siebel Open UI
Customizing the Presentation Model and Physical Renderer in Siebel Open UICustomizing the Presentation Model and Physical Renderer in Siebel Open UI
Customizing the Presentation Model and Physical Renderer in Siebel Open UI
 
Javascript patterns
Javascript patternsJavascript patterns
Javascript patterns
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
S313937 cdi dochez
S313937 cdi dochezS313937 cdi dochez
S313937 cdi dochez
 

Más de FITC

Cut it up
Cut it upCut it up
Cut it upFITC
 
Designing for Digital Health
Designing for Digital HealthDesigning for Digital Health
Designing for Digital HealthFITC
 
Profiling JavaScript Performance
Profiling JavaScript PerformanceProfiling JavaScript Performance
Profiling JavaScript PerformanceFITC
 
Surviving Your Tech Stack
Surviving Your Tech StackSurviving Your Tech Stack
Surviving Your Tech StackFITC
 
How to Pitch Your First AR Project
How to Pitch Your First AR ProjectHow to Pitch Your First AR Project
How to Pitch Your First AR ProjectFITC
 
Start by Understanding the Problem, Not by Delivering the Answer
Start by Understanding the Problem, Not by Delivering the AnswerStart by Understanding the Problem, Not by Delivering the Answer
Start by Understanding the Problem, Not by Delivering the AnswerFITC
 
Cocaine to Carrots: The Art of Telling Someone Else’s Story
Cocaine to Carrots: The Art of Telling Someone Else’s StoryCocaine to Carrots: The Art of Telling Someone Else’s Story
Cocaine to Carrots: The Art of Telling Someone Else’s StoryFITC
 
Everyday Innovation
Everyday InnovationEveryday Innovation
Everyday InnovationFITC
 
HyperLight Websites
HyperLight WebsitesHyperLight Websites
HyperLight WebsitesFITC
 
Everything is Terrifying
Everything is TerrifyingEverything is Terrifying
Everything is TerrifyingFITC
 
Post-Earth Visions: Designing for Space and the Future Human
Post-Earth Visions: Designing for Space and the Future HumanPost-Earth Visions: Designing for Space and the Future Human
Post-Earth Visions: Designing for Space and the Future HumanFITC
 
The Rise of the Creative Social Influencer (and How to Become One)
The Rise of the Creative Social Influencer (and How to Become One)The Rise of the Creative Social Influencer (and How to Become One)
The Rise of the Creative Social Influencer (and How to Become One)FITC
 
East of the Rockies: Developing an AR Game
East of the Rockies: Developing an AR GameEast of the Rockies: Developing an AR Game
East of the Rockies: Developing an AR GameFITC
 
Creating a Proactive Healthcare System
Creating a Proactive Healthcare SystemCreating a Proactive Healthcare System
Creating a Proactive Healthcare SystemFITC
 
World Transformation: The Secret Agenda of Product Design
World Transformation: The Secret Agenda of Product DesignWorld Transformation: The Secret Agenda of Product Design
World Transformation: The Secret Agenda of Product DesignFITC
 
The Power of Now
The Power of NowThe Power of Now
The Power of NowFITC
 
High Performance PWAs
High Performance PWAsHigh Performance PWAs
High Performance PWAsFITC
 
Rise of the JAMstack
Rise of the JAMstackRise of the JAMstack
Rise of the JAMstackFITC
 
From Closed to Open: A Journey of Self Discovery
From Closed to Open: A Journey of Self DiscoveryFrom Closed to Open: A Journey of Self Discovery
From Closed to Open: A Journey of Self DiscoveryFITC
 
Projects Ain’t Nobody Got Time For
Projects Ain’t Nobody Got Time ForProjects Ain’t Nobody Got Time For
Projects Ain’t Nobody Got Time ForFITC
 

Más de FITC (20)

Cut it up
Cut it upCut it up
Cut it up
 
Designing for Digital Health
Designing for Digital HealthDesigning for Digital Health
Designing for Digital Health
 
Profiling JavaScript Performance
Profiling JavaScript PerformanceProfiling JavaScript Performance
Profiling JavaScript Performance
 
Surviving Your Tech Stack
Surviving Your Tech StackSurviving Your Tech Stack
Surviving Your Tech Stack
 
How to Pitch Your First AR Project
How to Pitch Your First AR ProjectHow to Pitch Your First AR Project
How to Pitch Your First AR Project
 
Start by Understanding the Problem, Not by Delivering the Answer
Start by Understanding the Problem, Not by Delivering the AnswerStart by Understanding the Problem, Not by Delivering the Answer
Start by Understanding the Problem, Not by Delivering the Answer
 
Cocaine to Carrots: The Art of Telling Someone Else’s Story
Cocaine to Carrots: The Art of Telling Someone Else’s StoryCocaine to Carrots: The Art of Telling Someone Else’s Story
Cocaine to Carrots: The Art of Telling Someone Else’s Story
 
Everyday Innovation
Everyday InnovationEveryday Innovation
Everyday Innovation
 
HyperLight Websites
HyperLight WebsitesHyperLight Websites
HyperLight Websites
 
Everything is Terrifying
Everything is TerrifyingEverything is Terrifying
Everything is Terrifying
 
Post-Earth Visions: Designing for Space and the Future Human
Post-Earth Visions: Designing for Space and the Future HumanPost-Earth Visions: Designing for Space and the Future Human
Post-Earth Visions: Designing for Space and the Future Human
 
The Rise of the Creative Social Influencer (and How to Become One)
The Rise of the Creative Social Influencer (and How to Become One)The Rise of the Creative Social Influencer (and How to Become One)
The Rise of the Creative Social Influencer (and How to Become One)
 
East of the Rockies: Developing an AR Game
East of the Rockies: Developing an AR GameEast of the Rockies: Developing an AR Game
East of the Rockies: Developing an AR Game
 
Creating a Proactive Healthcare System
Creating a Proactive Healthcare SystemCreating a Proactive Healthcare System
Creating a Proactive Healthcare System
 
World Transformation: The Secret Agenda of Product Design
World Transformation: The Secret Agenda of Product DesignWorld Transformation: The Secret Agenda of Product Design
World Transformation: The Secret Agenda of Product Design
 
The Power of Now
The Power of NowThe Power of Now
The Power of Now
 
High Performance PWAs
High Performance PWAsHigh Performance PWAs
High Performance PWAs
 
Rise of the JAMstack
Rise of the JAMstackRise of the JAMstack
Rise of the JAMstack
 
From Closed to Open: A Journey of Self Discovery
From Closed to Open: A Journey of Self DiscoveryFrom Closed to Open: A Journey of Self Discovery
From Closed to Open: A Journey of Self Discovery
 
Projects Ain’t Nobody Got Time For
Projects Ain’t Nobody Got Time ForProjects Ain’t Nobody Got Time For
Projects Ain’t Nobody Got Time For
 

Último

Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 

Último (20)

Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 

ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz

  • 1. ECMA5 approach to writing JavaScript frameworks Anzor Bashkhaz @anzor_b Isaac Gordezky @igordezky
  • 2. ECMA5 What is ECMA5? • ECMAScript = language of JavaScript • Final draft for v5 presented in April 2009 • Object creation/manipulation • Supported in all major platforms http://kangax.github.io/es5-compat-table/
  • 3. API design What is an API? • API exposes ways to use a library/framework • Ex: underscore • Users of API can’t see implementation • aka. Public API
  • 4. API: Real world example
  • 5. Dissecting the JavaScript Object: Type and Prototype 5
  • 6. Type What is a type? • Instance factory • JavaScript ‘Object’ is a type • Made up of: • Constructor (returns new instance) • Static methods (create, call,…) • Prototype
  • 7. Type What is a type? • Instance factory • JavaScript ‘Object’ is a type • Made up of: • Constructor (returns new instance) • Static methods (create, call,…) • Prototype
  • 8. Prototype What is a prototype? • Prototype = definition for new instances • = Public API • Object.prototype
  • 9. Prototype What is a prototype? • Prototype = definition for new instances • = Public API • Object.prototype
  • 10. Inside an instance Ex: Dissecting the instance of Object var foo = { hello : function(){ return “hello world”; } }; console.log(foo);
  • 11. Inside an instance Ex: Dissecting the instance of Object var foo = { hello : function(){ return “hello world”; } }; console.log(foo);
  • 12. Inside an instance Ex: Dissecting the instance of Object var foo = { hello : function(){ return “hello world”; } }; console.log(foo); What is __proto__?
  • 13. __proto__ “Special” property on every JavaScript Object. • foo was created from Object. var foo = {} • __proto__ of foo points at prototype of Object. • Object.create(definition) • Returns object (instance) with __proto__ pointing at definition • Returned object can be Object.create’d again and again
  • 14. __proto__ Ex: Prototype chain and simple inheritance var foo = {}; var bar = Object.create(foo);
  • 15. __proto__ Ex: Prototype chain and simple inheritance var foo = {}; var bar = Object.create(foo); bar inherits methods from foo, while foo inherits methods from Object.
  • 17. Custom Types Ex: create type with name “type<Shape>” var Shape = {}; Shape.constructor = { name: “type<Shape>" }; • Makes it easier to read in console versus “Object”
  • 18. Custom Types Ex: Add a prototype so instance are called “Shape” • Add a prototype property Shape.prototype = {}; • Set prototype.constructor.name to “Shape” Shape.prototype.constructor = { name: “Shape” };
  • 19. Custom Types – constructor Ex: Add a constructor that returns instance of Shape • Object.create() creates a new unique instance of Shape Shape.create = function(){ return Object.create(Shape.prototype); }; var shape1 = Shape.create(); var shape2 = Shape.create(); shape1.color = “blue”;
  • 20. Custom Types – constructor Ex: Add a constructor that returns instance of Shape • Object.create() creates a new unique instance of Shape Shape.create = function(){ return Object.create(Shape.prototype); }; var shape1 = Shape.create(); var shape2 = Shape.create(); shape1.color = “blue”;
  • 22. Defining Properties • Good API design = preventing unintended use • Read-only properties Object.defineProperty(object, propertyName, descriptor) • Descriptor • Enumerable • Configurable • Writable • Value Read more at MDN (http://mzl.la/1cCtwPN)
  • 23. Defining Properties • Enumerable true if the property is present while iterating through the object. (for prop in obj). Defaults to false. • Configurable true if the property descriptor can be modified or property deleted on object. Defaults to false. • Writable true if the property’s value can be changed? ( radius= 20) Defaults to false. • Value Value of the property
  • 24. Defining Properties var Shape = {}; Object.defineProperty(Shape, “constructor”, { value: { name: “type<Shape>” } }); Object.defineProperty(Shape, “prototype”, { value: {} }); Object.defineProperty(Shape.prototype, “constructor”, { value: { name: “Shape” } });
  • 27. Private Ex: Add a private area to instances of Shape Shape.create = function() { var privateArea = { public : Object.create(Shape.prototype), secret : 5, } return privateArea.public; }; var shape1 = Shape.create(); shape1.color = “blue”;
  • 28. Private Ex: Add methods to inspect the private area Shape.create = function() { var privateArea = { public : Object.create(Shape.prototype), secret : Math.floor((Math.random() * 10) + 1), //random secret per instance privateInspect : function() { console.log(this); //this points at instance } }; //public inspect function privateArea.public.inspect = function() { return privateArea.privateInspect(); }; return privateArea.public; };
  • 31. Getters and Setters • Important concept in framework design (especially UI) • Backbone.js uses “set()” method Ex: model.set(propertyName, value); > “change” event triggered > Views update their UI • Ideally we want: shape1.color = “blue”; > This should trigger a color change (using DOM/canvas) • Possible with Object.defineProperty ECMA5 way to define setters/getters
  • 32. Getters and Setters Ex: Define setter for color to notify of change var circle = {}, var currentValue; Object.defineProperty(circle, “color”, { get: function(){ return currentValue; }, set : function(value){ currentValue = value; console.log(“color changed to “ + value); } });
  • 33. Getters and Setters Ex: Define setter for color to notify of change var circle = {}, var currentValue; Object.defineProperty(circle, “color”, { get: function(){ return currentValue; }, set : function(value){ currentValue = value; console.log(“color changed to “ + value); } });
  • 34. Getters and Setters Ex: Helper function for defining properties var defineProperty = function(object, propertyName) { Object.defineProperty(object, propertyName, { get : function() { return object[propertyName]; }, set : function(value) { object[propertyName] = value; console.log(propertyName + "changed to " + value); } }); }; var circle2 = {}; defineProperty(circle2, "color");
  • 35. Beyond Private Covered so far: Public • Leverages inheritance pattern • Final instance has access to all methods in __proto__ chain Private • Instance specific data unexposed to the outside What about inheriting non-public methods? Protected to the rescue! Protected • Leverages inheritance pattern • Allows child types to re-implement methods from their parents. • Provides non-public APIs to child types.
  • 37. extend! • Declarative syntax for public, private and protected functions • hooks for init and destroy • Public values are defined as: • Property getters/setters on public • Property stored in privateArea • Non-functions in public create notifiable properties Ex: size triggers sizeChanged() • All methods have ‘this’ pointing to the privateArea • Uses require.js’ plugin system to hide parser
  • 38. Case Study: A Simple Class System 38
  • 39. Shapes Objective: Build an API to draw shapes using HTML5 canvas. type<Shape> • Manage canvas and DOM • Properties: color • Protected interface: resize • Abstract interface: draw type<Circle> extends type<Shape> • Implements draw interface • Properties: radius • Leverages resize interface
  • 40. Shape.color: a property Shape.color (Public) • Property available on any shape object Shape.colorChanged (Private) • Automatically triggered when the color property is changed • Triggers a re-draw Shape.color (Private) • Stores the value of color in Shape’s privateArea • Allows Shape’s methods to access color without the setter/getter
  • 41. Shape.resize: a protected interface Size • Public read-only property of Shape • Stored in the privateArea of Shape Resize • Exposed to child types through protected • Sets size in the privateArea of Shape • Resizes DOM elements Radius • Public property of Circle • Triggers protected Shape.resize
  • 42. Shape.draw: a virtual method Shape.draw (Public) • Public draw API for all Shapes • Calls virtual draw with canvas context from Shape’s privateArea Shape.draw (Protected) • Abstract protected draw function must be overridden by child classes Circle.draw (Protected) • Overrides Shape.draw (Protected) • Doesn’t know or care where context came from

Notas del editor

  1. Since the purpose of a type is to create an instance out of the protoype, our Shape type needs a create function, that returns a new instance of a shape.(We need to Object.create the prototype, otherwise every instance of shape will point at the same prototype instead of being separate instances.)Next, we’ll create two instances of Shape and assign a blue color to one.[var shape1 = Shape.create();var shape2 = Shape.create();shape1.color = “blue”;&gt;shape1&gt;shape2](Both shapes have a color property of blue, and this is because they both point at the same object (Shape.prototype).What we need to do is to Object.create(Shape.prototype).)Now only shape1 has a color of blue, which is the desired effect.So we have our Shape type which spawns shape instances, but there are a few problems.The constructor object is visible and users can edit it. This will lead to unintended use of the type or instance, which needs to be avoided when desiging a good API. Therefore, we need to lock constructor so users are not able to override it or delete it. This brings us to the next section: Property Definitions.
  2. Since the purpose of a type is to create an instance out of the protoype, our Shape type needs a create function, that returns a new instance of a shape.(We need to Object.create the prototype, otherwise every instance of shape will point at the same prototype instead of being separate instances.)Next, we’ll create two instances of Shape and assign a blue color to one.[var shape1 = Shape.create();var shape2 = Shape.create();shape1.color = “blue”;&gt;shape1&gt;shape2](Both shapes have a color property of blue, and this is because they both point at the same object (Shape.prototype).What we need to do is to Object.create(Shape.prototype).)Now only shape1 has a color of blue, which is the desired effect.So we have our Shape type which spawns shape instances, but there are a few problems.The constructor object is visible and users can edit it. This will lead to unintended use of the type or instance, which needs to be avoided when desiging a good API. Therefore, we need to lock constructor so users are not able to override it or delete it. This brings us to the next section: Property Definitions.
  3. After we have sensed the user, the device and its surrounding environment comes UNDERSTAND. What it really means is we need to take all our senses together and marry them with data on the device and the cloud and interpret those senses in our app’s context. This could be as simple as reading PIM data on the device to all the way up to retrieving relevant data from the cloud. Let me show you what we’ve done to enable this.
  4. The purpose of this session is to go over patterns that help design good APIs using tools provided by ECMA5.