ES6 Proxy Function Call Trap

Edwin Reynoso eorroe at gmail.com
Tue Jun 9 05:58:29 UTC 2015


Let's say we have the following code:

```
class Person {
constructor(fName, lName) {
this.firstName = fName;
this.lastName = lName;
}

logFullName() {
console.log(this.firstName, this.lastName);
}
}

var people = new Proxy([new Person('Edwin', 'Reynoso'), new Person('That',
'Guy')], {
get(target, property) {
if(target[property]) return target[property];
var arr = [];
for(var person of target) arr.push(person[property]);
return arr;
}
});

people.firstName // returns ["Edwin", "That"]
people.lastName // returns ["Reynoso", "Guy"]

people.logFullName // returns [Person.prototype.logFullName,
Person.prototype.logFullName]

people.forEach // returns forEach meaning any property of the acutal array
is returned
```

I can't call logFullName unless it was a function of the target which is
not (and not what I want bc I'll have to do that for every method)

Also I want the function to be called on each element if it could be called
on it, if not return `undefined`

So how about a call trap:

```
var people = new Proxy([new Person('Edwin', 'Reynoso'), new Person('That',
'Guy')], {
call(target, functionName, arguments) {
// target would be the array
// functionName the name of the function
// arguments the arguments passed in as an array
}
});
```

If the target has a method the `handler.get` of the proxy above would
return that method and therefore you can call it.
But what if no such method exist __nosuchmethod__ would be the property yet
how would we get the arguments?

Another thing that could possibly done is whenever the property is
__nosuchmethod__ pass another argument which would be the actual arguments


**Or does someone know how to do what I'm trying to accomplish**
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150609/b86d1546/attachment-0001.html>


More information about the es-discuss mailing list