4 ms·
Not too sure about what the point is, or maybe it is just my limited knowledge of javascirpt that's playing me tricks. In any case I didn't know of a prototype.
by igrekel 15y ago
Not too sure about what the point is, or maybe it is just my limited knowledge of javascirpt that's playing me tricks. In any case I didn't know of a prototype.handle function and can't figure out any way for it to intervene in the usual way of calling functions on an object.
var f=new Foo()
f.bar() //Just says that f.bar is undefined and therefor not a function
//If I add
f.bar=function(something){return "nothing"}
//Calling
f.bar("ABC") //Many times always answers "nothing"
- gliese1337 15y agoThere isn't such a function built-in, though something like it (a doNotUnderstand functionality to call a default function instead of throwing an error if the requested field isn't found on an object) has been proposed as a future addition to the JavaScript standard. IgorPartola's example code won't intervene in the usual function-call process; rather, it provides a tacked-on means of simulating message sending behavior that ought to be included in the language's syntax under Kay's definition of OO. With this convention, you'd never call bar(), or any other method, directly; you'd call handle to send a 'bar' message to the object instead. Then, if you send a message the object doesn't understand, handle() can route it properly.
- justincormack 15y agoLua has this, with metatables that can set methods to handle undefined stuff. It is actually very useful and you can do a lot with it.