No estándar
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Obsoleto Gecko 43 (Firefox 43 / Thunderbird 43 / SeaMonkey 2.40)
Esta funcionalidad es obsoleta. Aunque puede aún funcionar en algunos navegadores, se desalienta su uso ya que puede ser removida en cualquier momento. Evite usarla.
No estándar
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Obsoleto
Esta funcionalidad es obsoleta. Aunque puede aún funcionar en algunos navegadores, se desalienta su uso ya que puede ser removida en cualquier momento. Evite usarla.
La __noSuchMethod__
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.
Mientras __noSuchMethod__
a sido eliminado, la especificación ECMAScript 2015 tiene el objeto Proxy
, el cual puede lograr lo siguiente (y más).
Sintaxis de data
//EXample 2 obj.__noSuchMetod__ = id
Otros ejemplos como fun
obj.__noSuchMethod__ = fun
Parameters
fun
- Una función que toma la forma
-
function (id, args) { . . . }
id
- El nombre del método inexistente que fue llamado
args
- Un array de los argumentos pasados al método
Descripción
Por defecto, un intento de llamar a un método que no existe en un objeto tiene como resultado TypeError
. Este comportamiento puede evitarse definiendo una función en el miembro __noSuchMethod__
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 Array.prototype
) y no el objeto array con el objeto arguments.
Si no se puede llamar a este método, ya sea undefined
por defecto, como si se hubiera eliminado, o si se ha configurado manualmente como no funcional, el motor JavaScript volverá a lanzar TypeError
s.
Esto es creado por esto:
Director de traductor de grupos:
Vicente Oliver
titox31
Ejemplos
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
A continuación se muestra un ejemplo de código que implementa una forma primitiva de la herencia múltiple.
// 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);
}
Un ejemplo de cómo utilizar esta idea se muestra a continuación.
// 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
Specifications
No forma parte de ninguna especificación. Esta característica ha sido eliminada, vea error 683218.
Alias de __noSuchMetod__
aliasduplicate.lopen_bash
hamlStyluslopen
Browser compatibility
No compatibility data found. Please contribute data for "javascript.builtins.Object.noSuchMethod" (depth: 1) to the MDN compatibility data repository.