Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Compare Revisions

Object.prototype.__noSuchMethod__

Change Revisions

Revision 72091:

Revision 72091 by tmim on

Revision 72092:

Revision 72092 by Sheppy on

Title:
__noSuchMethod__
__noSuchMethod__
Slug:
JavaScript/Reference/Global_Objects/Object/noSuchMethod
JavaScript/Reference/Global_Objects/Object/noSuchMethod
Tags:
JavaScript, "JavaScript Reference"
JavaScript, "JavaScript Reference"
Content:

Revision 72091
Revision 72092
n10    <h2 name="Summary">n10    <h2 id="Summary" name="Summary">
n43    <h2 name="Syntax">n43    <h2 id="Syntax" name="Syntax">
n49    <h2 name="Parameters">n49    <h2 id="Parameters" name="Parameters">
n77    <h2 name="Description">n77    <h2 id="Description" name="Description">
n86    <h2 name="Example">n86    <h2 id="Example" name="Example">
n90      __noSuchMethod__ can be used to simulate multiple inheritann90      <code>__noSuchMethod__</code> can be used to simulate multi
>ce.>ple inheritance.
n93      The code that can implement a primitive form of multiple inn93      An example of code that implements a primitive form of mult
>heritance is shown below.>iple inheritance is shown below.
n95    <pre class="eval">n95    <pre class="brush: js">
96//Doesn't work with multiple inheritance objects as parents96// Doesn't work with multiple inheritance objects as parents
97 
97function noMethod(name, args){98function noMethod(name, args) {
98    var parents=this.__parents_;99  var parents=this.__parents_;
100  
99    //Go through all parents101  // Go through all parents
102  
100    for(var i=0;i&lt;parents.length;i++){103  for (var i=0;i&lt;parents.length;i++) {
101        //If we find a function on the parent, we call it104    // If we find a function on the parent, we call it
102        if(typeof parents[i][name] =="function") return parents[i105    if (typeof parents[i][name] =="function") {
>][name].apply(this, args); 
106      return parents[i][name].apply(this, args);
nn108  }
109  
104    //If we get here, the method hasn't been found110  // If we get here, the method hasn't been found
111  
105    throw new TypeError;112  throw new TypeError;
nn114 
107//Used to add a parent for multiple inheritance115// Used to add a parent for multiple inheritance
116 
108function addParent(obj, parent){117function addParent(obj, parent) {
109    //If object isn't initialised, initialize it118  // If the object isn't initialized, initialize it
119  
110    if(!obj.__parents_){120  if (!obj.__parents_) {
111        obj.__parents_=[];121    obj.__parents_=[];
112        obj.__noSuchMethod__=noMethod;122    obj.__noSuchMethod__=noMethod;
113    }123  }
124  
114    //Add the parent125  // Add the parent
126  
115    obj.__parents_.push(parent);127  obj.__parents_.push(parent);
n121    <pre class="eval">n133    <pre class="brush: js">
122//Example base class 1134// Example base class 1
135 
n124    this.name=name;n137  this.name=name;
nn139 
126NamedThing.prototype={140NamedThing.prototype = {
127    getName: function(){return this.name;},141  getName: function() {return this.name;},
128    setName: function(newName){this.name=newName;}142  setName: function(newName) {this.name=newName;}
nn146 
n133    this.age=age;n148  this.age=age;
134}
135AgedThing.prototype={
136    getAge: function(){return this.age;},
137    setAge: function(age){this.age=age;}
nn151AgedThing.prototype = {
152  getAge: function(){return this.age;},
153  setAge: function(age){this.age=age;}
154}
155 
140//Child class. inherits from NamedThing and AgedThing as well as 156// Child class. inherits from NamedThing and AgedThing as well as
>defining address> defining address
157 
n142    addParent(this, NamedThing.prototype);n159  addParent(this, NamedThing.prototype);
143    NamedThing.call(this, name);160  NamedThing.call(this, name);
144    addParent(this, AgedThing.prototype);161  addParent(this, AgedThing.prototype);
145    AgedThing.call(this, age);162  AgedThing.call(this, age);
146    this.address=address;163  this.address=address;
nn165 
148Person.prototype={166Person.prototype = {
149    getAddr: function(){return this.address;},167  getAddr: function() {return this.address;},
150    setAddr: function(addr){this.address=addr;}168  setAddr: function(addr) {this.address=addr;}
tt172 

Back to History