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.
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
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
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
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!