2009-06-20 6 views

Répondre

1

Vous pouvez passer l'appelant comme argument à la fonction:

function b(caller) { 
    alert(caller.x); 
}; 

function X(x) { 
    this.x = x; 
}; 

X.prototype.a = function(i) { 
    b(this); 
}; 

new X(10).a(5); 

Notez que arguments.caller est dépréciée en 1.3 JS et enlevé JS 1.5.

1
function b() { 
    alert(this.x); 
} 

function X(x) { 
    this.x = x; 
} 

X.prototype.a = function(i) { 
    b.call(this); /* <- call() used to specify context */ 
} 

new X(10).a(5); 
0

Vous introduisez un niveau d'indirection en enveloppant l'appel de la fonction b dans une fonction anonyme. Si possible, vous devez le définir directement.

function b() { 
    alert(this.x); // 10 
    alert(arguments[0]); // 5 
} 

function X(x) { 
    this.x = x; /* alternatively, set this.x = arguments to capture all arguments*/ 
} 

X.prototype.a = b; 

new X(10).a(5); 

Sinon, vous devrez passer l'objet, qui peut être fait dans l'une des manières J-P ou balpha suggéraient.