Hi all, anyone know if it''s possible to imitate the very useful PHP __call() method in protoype? For those who are not familiar with PHP, the __call() method is invoked whenever a method on an object is called and the method does not exist. For example in PHP: class MyClass { /** * @param string $method the name of the called method that did not exist. * @param array $params an array of parameters passed to the method that did not exist. */ public function __call($method, $params) { } } Any ideas? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
GarethAtFlignet wrote:> Hi all, > > anyone know if it''s possible to imitate the very useful PHP __call() > method in protoype? > > For those who are not familiar with PHP, the __call() method is > invoked whenever a method on an object is called and the method does > not exist. For example in PHP: > ... > > Any ideas? >I don''t think there is a normal way to do this in JavaScript at all. You can catch errors of invoking undefined methods with a window.onerror = function(errorString, urlString, lineNo){} but I don''t think there is a cross-browser way of resolving the object that triggered the error. What is the use case? Most of the cases I''ve use __call() in PHP it is to wrap classes that can''t be extended; but in JavaScript you can extend all classes using object.prototype. - Ken Snyder --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Pretty sure not possible, unless you don''t use dot/bracket notation for calling object method but substitute it with some analog of a "send" method: Object.prototype.__send = function(method){ ... } foo.__send(''nonexistent''); // intercept error Another alternative would be to wrap execution with try/catch, then parse error message in catch block (which is unfortunately different across browsers) and proceed as needed : ) - kangax On May 6, 9:57 am, GarethAtFlignet <garethatflig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > anyone know if it''s possible to imitate the very useful PHP __call() > method in protoype? > > For those who are not familiar with PHP, the __call() method is > invoked whenever a method on an object is called and the method does > not exist. For example in PHP: > > class MyClass { > > /** > * @param string $method the name of the called method that did not > exist. > * @param array $params an array of parameters passed to the method > that did not exist. > */ > public function __call($method, $params) { > > } > > } > > Any ideas?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
2008/5/6 GarethAtFlignet <garethatflignet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Hi all, > > anyone know if it''s possible to imitate the very useful PHP __call() > method in protoype? > > For those who are not familiar with PHP, the __call() method is > invoked whenever a method on an object is called and the method does > not exist. For example in PHP: > > class MyClass { > > /** > * @param string $method the name of the called method that did not > exist. > * @param array $params an array of parameters passed to the method > that did not exist. > */ > public function __call($method, $params) { > > } > } > > Any ideas?You would have to have a __call method (just like in PHP), but everything would need to go through it. myJSObj.__call(''method'', ''param'', ''param2'', ''param3''); Similar to PHP''s call_user_func(). You could also implement a call_user_method which took as the first param an object and a method name ... myJSObj.__call({obj:someObj, method:''someMethod''}, ''param1'', ''param2''); This would allow you to do static calls also. -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---