Object.prototype.__noSuchMethod__

  • Revision slug: JavaScript/Reference/Global_Objects/Object/noSuchMethod
  • Revision title: __noSuchMethod__
  • Revision id: 72080
  • Created:
  • Creator: Sevenspade
  • Is current revision? No
  • Comment /* Parameters */

Revision Content

{{wiki.template('Non-standard_header')}}

Summary

Executes a function when a non-existent method is called on an object.

Syntax

obj.__noSuchMethod__ = fun

Parameters

fun
a function that takes the form
function (id, args) { . . . }
id
the name of the non-existent method that was called
args
an array of the arguments passed to the method

Description

By default, an attempt to call a method that doesn't exist on an object results in a TypeError being thrown. This behavior can be circumvented by defining a function at that object's __noSuchMethod__ member. The function takes two arguments, the first is the name of the method that was attempted to call and the second is an array of the arguments that were passed in the method call. The second argument is an actual array (that is, it inherits through the Array prototype chain) and not the array-like arguments object.

If this method cannot be called, either as if undefined by default, if deleted, or if manually set to a non-function, the JavaScript engine will revert to throwing TypeErrors.

This currently only works when the non-existent method is called using the "dot" notation (obj.methodName()). See {{template.Bug(371033)}}.

Examples

Example: A basic usage of __noSuchMethod__

Suppose you have a project which has deprecated the use of an errorize method , both because errorize is poorly-named and because your new log method handles the severity of the occurrence, such as an error versus a simple warning. You could replace the functionality of errorize using __noSuchMethod__ to substitute a call to log:

wittyProjectName.log = function log (message, type) {
  if (type == 0) {
    // log an error
  }
  else if (type == 1) {
    // log a warning
  }
}

wittyProjectName.__noSuchMethod__ = function __noSuchMethod__ (id, args) {
  if (id == 'errorize') {
    wittyProjectName.log("wittyProjectName.errorize has been deprecated.\n" +
                         "Use wittyProjectName.log(message, 0) instead.", 1);
    wittyProjectName.log(args[0], 0); // log the message to the error log
  }
}

Revision Source

<p>
{{wiki.template('Non-standard_header')}}
</p>
<h3 name="Summary"> Summary </h3>
<p>Executes a function when a non-existent method is called on an object.
</p>
<h3 name="Syntax"> Syntax </h3>
<p><code><i>obj</i>.__noSuchMethod__ = <i>fun</i></code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>fun</code>
</dt><dd> a function that takes the form
</dd><dd> <code>function (<i>id</i>, <i>args</i>) { . . . }</code>
<dl><dt> <code>id</code>
</dt><dd> the name of the non-existent method that was called
</dd><dt> <code>args</code>
</dt><dd> an array of the arguments passed to the method
</dd></dl>
</dd></dl>
<h3 name="Description"> Description </h3>
<p>By default, an attempt to call a method that doesn't exist on an object results in a <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/TypeError">TypeError</a> being thrown. This behavior can be circumvented by defining a function at that object's <code>__noSuchMethod__</code> member. The function takes two arguments, the first is the name of the method that was attempted to call and the second is an array of the arguments that were passed in the method call. The second argument is an actual array (that is, it inherits through the <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/prototype">Array prototype</a> chain) and not the array-like <a href="en/Core_JavaScript_1.5_Reference/Functions/arguments">arguments object</a>.
</p><p>If this method cannot be called, either as if <code>undefined</code> by default, if deleted, or if manually set to a non-function, the JavaScript engine will revert to throwing <code>TypeError</code>s.
</p>
<div class="note">This currently only works when the non-existent method is called using the "dot" notation (<code><i>obj</i>.<i>methodName</i>())</code>. See {{template.Bug(371033)}}.</div>
<h3 name="Examples"> Examples </h3>
<h3 name="Example:_A_basic_usage_of___noSuchMethod__"> Example: A basic usage of <code>__noSuchMethod__</code> </h3>
<p>Suppose you have a project which has deprecated the use of an <code>errorize</code> method , both because <code>errorize</code> is poorly-named and because your new <code>log</code> method handles the severity of the occurrence, such as an error versus a simple warning. You could replace the functionality of <code>errorize</code> using <code>__noSuchMethod__</code> to substitute a call to <code>log</code>:
</p>
<pre class="eval">wittyProjectName.log = function log (message, type) {
  if (type == 0) {
    // log an error
  }
  else if (type == 1) {
    // log a warning
  }
}

wittyProjectName.__noSuchMethod__ = function __noSuchMethod__ (id, args) {
  if (id == 'errorize') {
    wittyProjectName.log("wittyProjectName.errorize has been deprecated.\n" +
                         "Use wittyProjectName.log(message, 0) instead.", 1);
    wittyProjectName.log(args[0], 0); // log the message to the error log
  }
}
</pre>
Revert to this revision