mozilla

Revision 492909 of Object.prototype.__noSuchMethod__

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/noSuchMethod
  • Revision title: Object.prototype.__noSuchMethod__
  • Revision id: 492909
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Object")}} {{ Non-standard_header }}

Summary

The __noSuchMethod__ property references a function to be executed 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 attempted 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.

Example

Example: Simple test of __noSuchMethod__

var o = {
  __noSuchMethod__: function (id, args) { console.log(id, '(' + args.join(', ') + ')'); }
};

o.foo(1,2,3);
o.bar(4,5);
o.baz();

// Output
"foo" "(1, 2, 3)"
"bar" "(4, 5)"
"baz" "()"

Using __noSuchMethod__ to simulate multiple inheritance

An example of code that implements a primitive form of multiple inheritance is shown below.

// Doesn't work with multiple inheritance objects as parents

function noMethod(name, args) {
  var parents=this.__parents_;
  
  // Go through all parents
  
  for (var i=0;i<parents.length;i++) {
    // If we find a function on the parent, we call it
    if (typeof parents[i][name] =="function") {
      return parents[i][name].apply(this, args);
    }
  }
  
  // If we get here, the method hasn't been found
  
  throw new TypeError;
}

// Used to add a parent for multiple inheritance

function addParent(obj, parent) {
  // If the object isn't initialized, initialize it
  
  if (!obj.__parents_) {
    obj.__parents_=[];
    obj.__noSuchMethod__=noMethod;
  }
  
  // Add the parent
  
  obj.__parents_.push(parent);
}

An example of using this idea is shown below.

// Example base class 1

function NamedThing(name){
  this.name=name;
}

NamedThing.prototype = {
  getName: function() {return this.name;},
  setName: function(newName) {this.name=newName;}
}

//Example base class 2

function AgedThing(age){
  this.age=age;
}

AgedThing.prototype = {
  getAge: function(){return this.age;},
  setAge: function(age){this.age=age;}
}

// Child class. inherits from NamedThing and AgedThing as well as defining address

function Person(name, age, address){
  addParent(this, NamedThing.prototype);
  NamedThing.call(this, name);
  addParent(this, AgedThing.prototype);
  AgedThing.call(this, age);
  this.address=address;
}

Person.prototype = {
  getAddr: function() {return this.address;},
  setAddr: function(addr) {this.address=addr;}
}

var bob=new Person("bob", 25, "New York");

console.log("getAge is "+(("getAge" in bob)?"in":"not in")+" bob");
console.log("bob's age is: "+bob.getAge());
console.log("getName is "+(("getName" in bob)?"in":"not in")+" bob");
console.log("bob's name is: "+bob.getName());
console.log("getAddr is "+(("getAddr" in bob)?"in":"not in")+" bob");
console.log("bob's address is: "+bob.getAddr());

Specifications

Not part of any specifications.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatNo() }} {{ CompatGeckoDesktop("1.0") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatNo() }} {{ CompatNo() }} {{ CompatGeckoMobile("1.0") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

 

Revision Source

<div>
 {{JSRef("Global_Objects", "Object")}} {{ Non-standard_header }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <strong><code>__noSuchMethod__</code></strong> property references a function to be executed when a non-existent method is called on an object.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>obj</em>.__noSuchMethod__ = <em>fun</em></code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>fun</code></dt>
 <dd>
  A function that takes the form</dd>
 <dd>
  <pre>
<code>function (<em>id</em>, <em>args</em>) { . . . }</code></pre>
  <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>
<h2 id="Description" name="Description">Description</h2>
<p>By default, an attempt to call a method that doesn't exist on an object results in a <a href="/en-US/docs/JavaScript/Reference/Global_Objects/TypeError" title="JavaScript/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 attempted 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-US/docs/JavaScript/Reference/Global_Objects/Array/prototype" title="JavaScript/Reference/Global_Objects/Array/prototype">Array prototype</a> chain) and not the array-like <a href="/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments" title="JavaScript/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>
<h2 id="Example" name="Example">Example</h2>
<h3 id="Example.3A_Simple_test_of___noSuchMethod__">Example: Simple test of <code>__noSuchMethod__</code></h3>
<pre class="bz_comment_text" id="comment_text_51">
var o = {
  __noSuchMethod__: function (id, args) { console.log(id, '(' + args.join(', ') + ')'); }
};

o.foo(1,2,3);
o.bar(4,5);
o.baz();

// Output
"foo" "(1, 2, 3)"
"bar" "(4, 5)"
"baz" "()"

</pre>
<h3 id="Using___noSuchMethod___to_simulate_multiple_inheritance">Using <code>__noSuchMethod__</code> to simulate multiple inheritance</h3>
<p>An example of code that implements a primitive form of multiple inheritance is shown below.</p>
<pre class="brush: js">
// Doesn't work with multiple inheritance objects as parents

function noMethod(name, args) {
  var parents=this.__parents_;
  
  // Go through all parents
  
  for (var i=0;i&lt;parents.length;i++) {
    // If we find a function on the parent, we call it
    if (typeof parents[i][name] =="function") {
      return parents[i][name].apply(this, args);
    }
  }
  
  // If we get here, the method hasn't been found
  
  throw new TypeError;
}

// Used to add a parent for multiple inheritance

function addParent(obj, parent) {
  // If the object isn't initialized, initialize it
  
  if (!obj.__parents_) {
    obj.__parents_=[];
    obj.__noSuchMethod__=noMethod;
  }
  
  // Add the parent
  
  obj.__parents_.push(parent);
}
</pre>
<p>An example of using this idea is shown below.</p>
<pre class="brush: js">
// Example base class 1

function NamedThing(name){
  this.name=name;
}

NamedThing.prototype = {
  getName: function() {return this.name;},
  setName: function(newName) {this.name=newName;}
}

//Example base class 2

function AgedThing(age){
  this.age=age;
}

AgedThing.prototype = {
  getAge: function(){return this.age;},
  setAge: function(age){this.age=age;}
}

// Child class. inherits from NamedThing and AgedThing as well as defining address

function Person(name, age, address){
  addParent(this, NamedThing.prototype);
  NamedThing.call(this, name);
  addParent(this, AgedThing.prototype);
  AgedThing.call(this, age);
  this.address=address;
}

Person.prototype = {
  getAddr: function() {return this.address;},
  setAddr: function(addr) {this.address=addr;}
}

var bob=new Person("bob", 25, "New York");

console.log("getAge is "+(("getAge" in bob)?"in":"not in")+" bob");
console.log("bob's age is: "+bob.getAge());
console.log("getName is "+(("getName" in bob)?"in":"not in")+" bob");
console.log("bob's name is: "+bob.getName());
console.log("getAddr is "+(("getAddr" in bob)?"in":"not in")+" bob");
console.log("bob's address is: "+bob.getAddr());</pre>
<h2 id="Specifications">Specifications</h2>
<p>Not part of any specifications.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatGeckoDesktop("1.0") }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatGeckoMobile("1.0") }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<p>&nbsp;</p>
<!-- languages({
"fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/noSuchMethod"
}) -->
Revert to this revision