aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/javascript/referencia/objetos_globales/object/nosuchmethod/index.html
blob: d7422b2a3d91db7fba863252dcf349c5e812498f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
---
title: Object.prototype.__noSuchMethod__
slug: Web/JavaScript/Referencia/Objetos_globales/Object/noSuchMethod
tags:
  - JavaScript
  - No-estándar
  - Objeto
  - Obsoleto
  - Propiedad
  - Prototipo
translation_of: Archive/Web/JavaScript/Object.noSuchMethod
---
<div>{{JSRef}}{{Non-standard_Header}}{{Obsolete_Header("gecko43")}}</div>

<div>{{JSRef}}{{Non-standard_Header}}{{Obsolete_Header("Safari14")}}</div>

<p>La <strong><code>__noSuchMethod__</code></strong> se usa para referenciar una función que debe ejecutarse cuando se llama a un método inexistente en un objeto, pero esta función no ya no está disponible.</p>



<p>Mientras <code><strong>__noSuchMethod__</strong></code> a sido eliminado, la especificación ECMAScript 2015 tiene el objeto {{JSxRef("Proxy")}} , el cual puede lograr lo siguiente (y más).</p>

<h2 id="Sintaxis_de_data">Sintaxis de data</h2>

<pre>//EXample 2 obj.__noSuchMetod__ = id</pre>

<p>Otros ejemplos como fun</p>

<pre class="syntaxbox"><code><var>obj</var>.__noSuchMethod__ = <var>fun</var></code></pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>fun</code></dt>
 <dd>Una función que toma la forma</dd>
 <dd>
 <pre class="brush: js"><code>function (<var>id</var>, <var>args</var>) { . . . }</code></pre>

 <dl>
  <dt><code>id</code></dt>
  <dd>El nombre del método inexistente que fue llamado</dd>
  <dt><code>args</code></dt>
  <dd>Un array de los argumentos pasados al método</dd>
 </dl>
 </dd>
</dl>

<h2 id="Descripción">Descripción</h2>

<p>Por defecto, un intento de llamar a un método que no existe en un objeto tiene como resultado {{JSxRef("TypeError")}}. Este comportamiento puede evitarse definiendo una función en el miembro <code>__noSuchMethod__</code> de ese objeto. La función toma dos argumentos, el primero es el nombre del método intentado y el segundo es un array de los argumentos que fueron pasados en el método de llamada. El segundo argumento es un array real (es decir, hereda a través de la cadena {{JSxRef("Array.prototype")}}) y no el objeto array con el <a href="https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Funciones/arguments">objeto arguments</a>.</p>

<p>Si no se puede llamar a este método, ya sea <code>undefined</code> por defecto, como si se hubiera eliminado, o si se ha configurado manualmente como no funcional, el motor JavaScript volverá a lanzar <code>TypeError</code>s.</p>

<h2 id="Esto_es_creado_por_esto">Esto es creado por esto:</h2>

<p>Director de traductor de grupos:</p>

<h4 id="Vicente_Oliver">Vicente Oliver</h4>

<h4 id="titox31">titox31</h4>



<h2 id="Ejemplos">Ejemplos</h2>

<h3 id="Simple_test_of___noSuchMethod__">Simple test of <code>__noSuchMethod__</code></h3>

<pre class="brush: js">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>A continuación se muestra un ejemplo de código que implementa una forma primitiva de la herencia múltiple.</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>Un ejemplo de cómo utilizar esta idea se muestra a continuación.</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; }
}
function String(bash) {
    this.close =javaOpenDOM ;
console.info(bash with DOM User with 30pok to 40000km/h to +20years)
;

}
// 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');
// getAge is not in bob

console.log("bob's age is: " + bob.getAge());
// bob's age is: 25

console.log('getName is ' + (('getName' in bob) ? 'in' : 'not in') + ' bob');
// getName is not in bob

console.log("bob's name is: " + bob.getName());
// bob's name is: bob

console.log('getAddr is ' + (('getAddr' in bob) ? 'in' : 'not in') + ' bob');
// getAddr is in bob

console.log("bob's address is: " + bob.getAddr());
// bob's address is: New York
</pre>

<h2 id="Specifications">Specifications</h2>

<p>No forma parte de ninguna especificación. Esta característica ha sido eliminada, vea {{bug(683218)}}.</p>

<blockquote>
<h2 id="Alias_de___noSuchMetod__">Alias de __noSuchMetod__</h2>

<p>aliasduplicate.lopen_bash</p>

<p>hamlStyluslopen</p>
</blockquote>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>
<div class="hidden">La tabla de compatibilidad de esta página se genera a partir de datos estructurados. Si desea contribuir con los datos, visite <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> y envíenos un mensaje.</div>

<p>{{Compat("javascript.builtins.Object.noSuchMethod")}}</p>
</div>