Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Harmony Proxies in Node.js

simplereach
September 19, 2012

Harmony Proxies in Node.js

Russell Bradberry's presentation for the New York Node.js Meetup.

simplereach

September 19, 2012
Tweet

More Decks by simplereach

Other Decks in Programming

Transcript

  1. W pr ? • Provides access to the hidden underlying

    methods of an object • Lets you define how the object will act • After created, object handlers are protected • Proxy.create(handler, proto) => Object instances • Proxy.createFunction(handler, callTrap, constructTrap) => Functions & Classes
  2. Tr p ( r) Required Traps Optional Traps • getOwnPropertyDescriptor

    • getPropertyDescriptor • getOwnPropertyNames • getPropertyNames • definProperty • delete • fix • has • hasOwn • get • set • enumerate • keys
  3. R q r Tr p • getOwnPropertyDescriptor => function(name){ ...

    } Object.getOwnPropertyDescriptor(obj, name); Return a property descriptor for the object • getPropertyDescriptor => function(name){ ... } (not in ES5) Doesn't exist in NodeJS (ES5) so use same definition as getOwnPropertyDescriptor • getOwnPropertyNames => function(){ ... } Object.getOwnPropertyNames(obj); Return a list of property names • getPropertyNames => function(){ ... } (not in ES5) Doesn't exist in NodeJS(ES5) so use same definition as getOwnPropertyNames • defineProperty => function(name, descriptor){ ... } What to do when someone does "proxy.foo = 'bar'" • delete => function(name) { ... } What to do when someone calls "delete proxy.foo" • fix => function(){ ... } What happens when you call Object[freeze|seal|preventExtensions](proxy)
  4. Op Tr p • has => function(name) { ... }

    Return a boolean indicating the key exists in the object Defaults to "name in obj" • hasOwn => function(name){ ... } Return a boolean if this property exists as an "own" object Defaults to "Object.prototype.hasOwnProperty.call(obj, name)" • get => function(receiver, name){ ... } Receiver is the proxy name is the method or property to get Defaults to "return myobj[name]" • set => function(receiver, name, value){ ... } Receiver is the proxy Name is the key being set Value is the value to set to Defaults to "myObj[name] = value" • enumerate => function(){ ... } Return an array of keys to enumerate over Defaults to "var a = []; for(var k in myObj){ a.push(k); } return a;" • keys => function(){ ... } What to return when Object.keys is called Defaults to "return Object.keys(myObj)"
  5. Pr . r var myObj = { foo:'bar', answer:null };

    var handler = { //forward all required traps to myObj get:function(rec, name){ return myObj[name] || function(){ console.log(name, 'is missing'); return undefined; } } }; var proxy = Proxy.create(handler, myObj); myObj.foo => 'bar' proxy.foo => 'bar' myObj.bar() => undefined > "bar is missing"
  6. Pr . r F var myObj = { foo:'bar' };

    var construct = function(){ this.baz = 'qux' }; var handler = { //forward all required traps to myObj }; var MyProxy = Proxy.createFunction(handler, construct); MyProxy.foo => 'bar' new MyProxy() => { baz: 'qux' }
  7. Pr Mocking JS Objects: • https://github.com/devdazed/supermock • Mocks anything •

    Any undefined property returns a new mock • Any undefined method also returns a new mock • Mocks are named according to their position in the object hierarchy • Built in assertion testing $ npm install supermock $ node --harmony-proxies > SuperMock = require('supermock').SuperMock > myMock = new SuperMock() [SuperMock: <anonymous>] > myMock.foo.bar.baz().qux.russ.is.awesome() [SuperMock: <anonymous>.foo.bar.baz().qux.russ.is.awesome()]
  8. Q ?