SlideShare una empresa de Scribd logo
1 de 28
Descargar para leer sin conexión
Manipulating Object
Behavior at Runtime
a talk by @andreiursan
at meetup.com/gib-js, June 19, Meetup
Exploratory Programming,
Hacking the System,
I'm a coder I can code
Motivation
● Wanting to write DRY code
○ Very DRY Code
Source code metrics RR#041, James’ example from Code Climate
Motivation
● Wanting to write DRY code
○ Very DRY Code
Source code metrics RR#041, James’ example from Code Climate
Don't Repeat Yourself Principle
"DRY says that every piece of system knowledge should have one
authoritative, unambiguous representation. Every piece of knowledge in the
development of something should have a single representation. A system's
knowledge is far broader than just its code. It refers to database schemas, test
plans, the build system, even documentation."
DRY => SOLID => Design Patterns
Source Orthogonality and the DRY Principle, Don't repeat yourself
Don't Repeat Yourself Principle
"DRY says that every piece of system knowledge should have one
authoritative, unambiguous representation. Every piece of knowledge in the
development of something should have a single representation. A system's
knowledge is far broader than just its code. It refers to database schemas, test
plans, the build system, even documentation."
DRY => SOLID => Design Patterns
Source Orthogonality and the DRY Principle, Don't repeat yourself
Mechanisms
Inheritance
Mixins
Traits
Object Composition
Problem Domain
Game Engines, Game Editors, Widget Systems, CMSs,
Interactive Systems.
(order does not imply anything)
Problem
As a content creator, I want to be able to create Custom
Entities in order to satisfy new use cases.
Desired Features
Ability to add/remove behavior of an component/entity
without changing the implementation.
Component Based System
Component Carcass
addComponent
removeComponent
handleMessage
MessageTRAP
Component Carcass
CC + View
View + Movement
View + Movement + Player
var asteroid = new ComponentCarcass();
Component Carcass
addComponent
removeComponent
handleMessage
MessageTRAP
Component Carcass
CC + View
View + Movement
View + Movement + Player
var asteroid = new ComponentCarcass();
asteroid.addComponent(new View(x=50, y=60));
asteroid.x // 50
Component Carcass
addComponent
removeComponent
handleMessage
MessageTRAP
Component Carcass
CC + View
View + Movement
View + Movement + Player
var asteroid = new ComponentCarcass();
asteroid.addComponent(new View(x, y));
asteroid.addComponent(new Movement());
Component Carcass
addComponent
removeComponent
handleMessage
MessageTRAP
Handling Dependencies
Component Carcass
addComponent
removeComponent
handleMessage
MessageTRAP
each component has a
delegate which will handle
dependencies
Handling Dependencies
Component Carcass
addComponent
removeComponent
handleMessage
MessageTRAP
when you add a
component to the
Carcass. You set the
component delegate
property to self (the
carcass self)
Handling Dependencies
Component Carcass
addComponent
removeComponent
handleMessage
MessageTRAP
messages from the
component to the delegate
are going to be Trapped by
the component carcass and
handle like any other
message.
Handling Messages
Component Carcass
addComponent
removeComponent
handleMessage
MessageTRAP
handleMessage will check
which component from the
"array of components" knows
how to respond to the
trapped message. It will
forward the message to the
correct component and return
the response
Component Carcass
CC + View
View + Movement
View + Movement + Player
var asteroid = new ComponentCarcass();
asteroid.addComponent(new View(x, y));
asteroid.addComponent(new Movement());
asteroid.addComponent(new Player());
Component Carcass
addComponent
removeComponent
handleMessage
MessageTRAP
Who Does This?
Unity3D
Crafty.js
many others
Who Does This?
Unity3D
Crafty.js
many others
//Paddles
Crafty.e("Paddle, 2D, DOM, Color, Multiway")
.color('rgb(255,0,0)')
.attr({ x: 20, y: 100, w: 10, h: 100 })
.multiway(4, { W: -90, S: 90 });
Crafty has a "Component Builders" community.
Source Crafty Github, Crafty Components
Who Does This?
Unity3D
Crafty.js
many others
How to do it in JavaScript
Traits.js
Define Prototype
Proxy
function makeColorTrait(col) {
return Trait({
color: function() { return col; }
});}
function makePoint(x, y) {
return Trait.create(Object.prototype,
Trait.compose(
makeColorTrait('red'),
Trait({ getX: function() { return x; },
getY: function() { return y; },
toString: function() { return ''+x+'@'+y; }
})));
}
var p = makePoint(0,2);
p.color() // 'red'
Source Traits.js
How to do it in JavaScript
Traits.js
Define Prototype
Proxy
How to do it in JavaScript
Traits.js
Define Prototype
Proxy
A new feature in
ECMAScript 6.
Flexible and
expressive
metaprogramming
system which is also
securable.
Proxy
var proxyObj = Proxy.create({
get: function(obj, name) {
return 'Hello, '+ name;
},
set: function(obj, name, value) {
var log = "Property " + name +
" doesn't like value " + value;
console.log(log);
return true;
}
});
proxyObj
> "TypeError"
proxyObj.baz
> Hello, baz
proxyObj.foo = -3
> Property foo doesn't like value -3
Proxy
var Stack = (function(){
var stack = [],
allowed = [ "push", "pop", "length" ];
return Proxy.create({
get: function(receiver, name){
if (allowed.indexOf(name) > -1){
if(typeof stack[name] == "function"){
return stack[name].bind(stack);
} else {
return stack[name];
}
} else {
return undefined;
}
}
});
});
var mystack = new Stack();
mystack.push("hi");
mystack.push("goodbye");
console.log(mystack.length); // 2
console.log(mystack[0]); // undefined
console.log(mystack.pop()); // "goodbye"
Source nczonline.net
some more examples
gist.github.com/andreiursan/5825603
Resources
1. [API] The new Proxy API (draft)
2. [MDN] Old Proxy API
3. Traits: Composable Units of Behavior
4. Delegation - The White Paper[Google Tech Talk]
5. Changes to ECMAScript - Proxies and Traits
6. Brendan Eich - Proxies are Awesome!
Thank you!
@andreiursan
meetup.com/gib-js, June 19, Meetup

Más contenido relacionado

La actualidad más candente

Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptMichael Girouard
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
Advanced Object-Oriented JavaScript
Advanced Object-Oriented JavaScriptAdvanced Object-Oriented JavaScript
Advanced Object-Oriented JavaScriptecker
 
Adopting Swift Generics
Adopting Swift GenericsAdopting Swift Generics
Adopting Swift GenericsMax Sokolov
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptNascenia IT
 
Real World Generics In Swift
Real World Generics In SwiftReal World Generics In Swift
Real World Generics In SwiftVadym Markov
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructorDa Mystic Sadi
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPTShubham Mondal
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Datagreenwop
 
Svcc Java2D And Groovy
Svcc Java2D And GroovySvcc Java2D And Groovy
Svcc Java2D And GroovyAndres Almiray
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++HalaiHansaika
 
Svcc Building Rich Applications with Groovy's SwingBuilder
Svcc Building Rich Applications with Groovy's SwingBuilderSvcc Building Rich Applications with Groovy's SwingBuilder
Svcc Building Rich Applications with Groovy's SwingBuilderAndres Almiray
 

La actualidad más candente (20)

Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Advanced Object-Oriented JavaScript
Advanced Object-Oriented JavaScriptAdvanced Object-Oriented JavaScript
Advanced Object-Oriented JavaScript
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
Adopting Swift Generics
Adopting Swift GenericsAdopting Swift Generics
Adopting Swift Generics
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Real World Generics In Swift
Real World Generics In SwiftReal World Generics In Swift
Real World Generics In Swift
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
Java Script Workshop
Java Script WorkshopJava Script Workshop
Java Script Workshop
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
 
Svcc Java2D And Groovy
Svcc Java2D And GroovySvcc Java2D And Groovy
Svcc Java2D And Groovy
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
 
Core concepts-javascript
Core concepts-javascriptCore concepts-javascript
Core concepts-javascript
 
Svcc Groovy Testing
Svcc Groovy TestingSvcc Groovy Testing
Svcc Groovy Testing
 
Svcc Building Rich Applications with Groovy's SwingBuilder
Svcc Building Rich Applications with Groovy's SwingBuilderSvcc Building Rich Applications with Groovy's SwingBuilder
Svcc Building Rich Applications with Groovy's SwingBuilder
 

Destacado (8)

Michelangelo Di Lodovico Buonarroti Simoni
Michelangelo Di Lodovico Buonarroti SimoniMichelangelo Di Lodovico Buonarroti Simoni
Michelangelo Di Lodovico Buonarroti Simoni
 
Easy Bus Rental Capaibilities
Easy Bus Rental CapaibilitiesEasy Bus Rental Capaibilities
Easy Bus Rental Capaibilities
 
弘一大師李叔同
弘一大師李叔同弘一大師李叔同
弘一大師李叔同
 
Globaltrendsineducation 100909000054 Phpapp02
Globaltrendsineducation 100909000054 Phpapp02Globaltrendsineducation 100909000054 Phpapp02
Globaltrendsineducation 100909000054 Phpapp02
 
Become a marketer
Become a marketerBecome a marketer
Become a marketer
 
FC Kuňava Baby
FC Kuňava BabyFC Kuňava Baby
FC Kuňava Baby
 
Orion presentation
Orion presentationOrion presentation
Orion presentation
 
Italianproject 111011102057-phpapp01
Italianproject 111011102057-phpapp01Italianproject 111011102057-phpapp01
Italianproject 111011102057-phpapp01
 

Similar a Manipulating object-behavior-at-runtime

Aleksandr Matrosov, Eugene Rodionov - HexRaysCodeXplorer make object-oriented...
Aleksandr Matrosov, Eugene Rodionov - HexRaysCodeXplorer make object-oriented...Aleksandr Matrosov, Eugene Rodionov - HexRaysCodeXplorer make object-oriented...
Aleksandr Matrosov, Eugene Rodionov - HexRaysCodeXplorer make object-oriented...DefconRussia
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsJeff Durta
 
Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder paramisoft
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoPaulo Morgado
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]Rome468
 
Using ReasonML For Your Next JavaScript Project
Using ReasonML For Your Next JavaScript ProjectUsing ReasonML For Your Next JavaScript Project
Using ReasonML For Your Next JavaScript ProjectRoy Derks
 
Designing REST API automation tests in Kotlin
Designing REST API automation tests in KotlinDesigning REST API automation tests in Kotlin
Designing REST API automation tests in KotlinDmitriy Sobko
 
Constance et qualité du code dans une équipe - Rémi Prévost
Constance et qualité du code dans une équipe - Rémi PrévostConstance et qualité du code dans une équipe - Rémi Prévost
Constance et qualité du code dans une équipe - Rémi PrévostWeb à Québec
 
Object Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerObject Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerAlex Matrosov
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom componentsJeremy Grancher
 
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰Jung Kim
 
Boost delivery stream with code discipline engineering
Boost delivery stream with code discipline engineeringBoost delivery stream with code discipline engineering
Boost delivery stream with code discipline engineeringMiro Wengner
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme SwiftMovel
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpg_hemanth17
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Sachin Singh
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpRaga Vahini
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpSatish Verma
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpsinghadarsh
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharpMody Farouk
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsKuntal Bhowmick
 

Similar a Manipulating object-behavior-at-runtime (20)

Aleksandr Matrosov, Eugene Rodionov - HexRaysCodeXplorer make object-oriented...
Aleksandr Matrosov, Eugene Rodionov - HexRaysCodeXplorer make object-oriented...Aleksandr Matrosov, Eugene Rodionov - HexRaysCodeXplorer make object-oriented...
Aleksandr Matrosov, Eugene Rodionov - HexRaysCodeXplorer make object-oriented...
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 
Using ReasonML For Your Next JavaScript Project
Using ReasonML For Your Next JavaScript ProjectUsing ReasonML For Your Next JavaScript Project
Using ReasonML For Your Next JavaScript Project
 
Designing REST API automation tests in Kotlin
Designing REST API automation tests in KotlinDesigning REST API automation tests in Kotlin
Designing REST API automation tests in Kotlin
 
Constance et qualité du code dans une équipe - Rémi Prévost
Constance et qualité du code dans une équipe - Rémi PrévostConstance et qualité du code dans une équipe - Rémi Prévost
Constance et qualité du code dans une équipe - Rémi Prévost
 
Object Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerObject Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorer
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom components
 
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
 
Boost delivery stream with code discipline engineering
Boost delivery stream with code discipline engineeringBoost delivery stream with code discipline engineering
Boost delivery stream with code discipline engineering
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharp
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methods
 

Último

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Último (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

Manipulating object-behavior-at-runtime

  • 1. Manipulating Object Behavior at Runtime a talk by @andreiursan at meetup.com/gib-js, June 19, Meetup
  • 2. Exploratory Programming, Hacking the System, I'm a coder I can code
  • 3. Motivation ● Wanting to write DRY code ○ Very DRY Code Source code metrics RR#041, James’ example from Code Climate
  • 4. Motivation ● Wanting to write DRY code ○ Very DRY Code Source code metrics RR#041, James’ example from Code Climate
  • 5. Don't Repeat Yourself Principle "DRY says that every piece of system knowledge should have one authoritative, unambiguous representation. Every piece of knowledge in the development of something should have a single representation. A system's knowledge is far broader than just its code. It refers to database schemas, test plans, the build system, even documentation." DRY => SOLID => Design Patterns Source Orthogonality and the DRY Principle, Don't repeat yourself
  • 6. Don't Repeat Yourself Principle "DRY says that every piece of system knowledge should have one authoritative, unambiguous representation. Every piece of knowledge in the development of something should have a single representation. A system's knowledge is far broader than just its code. It refers to database schemas, test plans, the build system, even documentation." DRY => SOLID => Design Patterns Source Orthogonality and the DRY Principle, Don't repeat yourself
  • 8. Problem Domain Game Engines, Game Editors, Widget Systems, CMSs, Interactive Systems. (order does not imply anything) Problem As a content creator, I want to be able to create Custom Entities in order to satisfy new use cases. Desired Features Ability to add/remove behavior of an component/entity without changing the implementation.
  • 9. Component Based System Component Carcass addComponent removeComponent handleMessage MessageTRAP
  • 10. Component Carcass CC + View View + Movement View + Movement + Player var asteroid = new ComponentCarcass(); Component Carcass addComponent removeComponent handleMessage MessageTRAP
  • 11. Component Carcass CC + View View + Movement View + Movement + Player var asteroid = new ComponentCarcass(); asteroid.addComponent(new View(x=50, y=60)); asteroid.x // 50 Component Carcass addComponent removeComponent handleMessage MessageTRAP
  • 12. Component Carcass CC + View View + Movement View + Movement + Player var asteroid = new ComponentCarcass(); asteroid.addComponent(new View(x, y)); asteroid.addComponent(new Movement()); Component Carcass addComponent removeComponent handleMessage MessageTRAP
  • 14. Handling Dependencies Component Carcass addComponent removeComponent handleMessage MessageTRAP when you add a component to the Carcass. You set the component delegate property to self (the carcass self)
  • 15. Handling Dependencies Component Carcass addComponent removeComponent handleMessage MessageTRAP messages from the component to the delegate are going to be Trapped by the component carcass and handle like any other message.
  • 16. Handling Messages Component Carcass addComponent removeComponent handleMessage MessageTRAP handleMessage will check which component from the "array of components" knows how to respond to the trapped message. It will forward the message to the correct component and return the response
  • 17. Component Carcass CC + View View + Movement View + Movement + Player var asteroid = new ComponentCarcass(); asteroid.addComponent(new View(x, y)); asteroid.addComponent(new Movement()); asteroid.addComponent(new Player()); Component Carcass addComponent removeComponent handleMessage MessageTRAP
  • 19. Who Does This? Unity3D Crafty.js many others //Paddles Crafty.e("Paddle, 2D, DOM, Color, Multiway") .color('rgb(255,0,0)') .attr({ x: 20, y: 100, w: 10, h: 100 }) .multiway(4, { W: -90, S: 90 }); Crafty has a "Component Builders" community. Source Crafty Github, Crafty Components
  • 21. How to do it in JavaScript Traits.js Define Prototype Proxy function makeColorTrait(col) { return Trait({ color: function() { return col; } });} function makePoint(x, y) { return Trait.create(Object.prototype, Trait.compose( makeColorTrait('red'), Trait({ getX: function() { return x; }, getY: function() { return y; }, toString: function() { return ''+x+'@'+y; } }))); } var p = makePoint(0,2); p.color() // 'red' Source Traits.js
  • 22. How to do it in JavaScript Traits.js Define Prototype Proxy
  • 23. How to do it in JavaScript Traits.js Define Prototype Proxy A new feature in ECMAScript 6. Flexible and expressive metaprogramming system which is also securable.
  • 24. Proxy var proxyObj = Proxy.create({ get: function(obj, name) { return 'Hello, '+ name; }, set: function(obj, name, value) { var log = "Property " + name + " doesn't like value " + value; console.log(log); return true; } }); proxyObj > "TypeError" proxyObj.baz > Hello, baz proxyObj.foo = -3 > Property foo doesn't like value -3
  • 25. Proxy var Stack = (function(){ var stack = [], allowed = [ "push", "pop", "length" ]; return Proxy.create({ get: function(receiver, name){ if (allowed.indexOf(name) > -1){ if(typeof stack[name] == "function"){ return stack[name].bind(stack); } else { return stack[name]; } } else { return undefined; } } }); }); var mystack = new Stack(); mystack.push("hi"); mystack.push("goodbye"); console.log(mystack.length); // 2 console.log(mystack[0]); // undefined console.log(mystack.pop()); // "goodbye" Source nczonline.net
  • 27. Resources 1. [API] The new Proxy API (draft) 2. [MDN] Old Proxy API 3. Traits: Composable Units of Behavior 4. Delegation - The White Paper[Google Tech Talk] 5. Changes to ECMAScript - Proxies and Traits 6. Brendan Eich - Proxies are Awesome!