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
} 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)
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)"
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()]