Questa è una domanda di follow-up dalla mia ultima domanda.prototipo JavaScript 'questo' numero
Simple javascript prototype issue
io sono un po 'nuova utilizzando JavaScript prototype
così dispiaciuto per il secondo posto.
Desidero assegnare l'elemento selezionato id
all'array this.name
.
task.prototype.init=function(){
this.name=[]; //this.name array has to be defined here
for (var i; i<5; i++){
var Link=document.createElement('a');
Link.innerHTML='click';
Link.id=value[i]; //I want to assign the value to the this.name array
Link.href='#'
Link.onclick=this.changeName;
document.body.appendChild(Link);
}
}
task.prototype.changeName=function(){
//How do I push the this.id to the property this.name?
//the code below won't work because this refer to the <a> element.
this.name.push(this.id);
return false;
}
Eventuali suggerimenti per l'attività?
Non utilizzare CapitalCase per le variabili regolari. – katspaugh
Possibile duplicato di [JavaScript Callback Scope] (http://stackoverflow.com/questions/183214/javascript-callback-scope) e [addEventListener e lo scopo di questo] (http://stackoverflow.com/questions/1803195/ addeventlistener-and-the-scope-of-this) e molti altri. – katspaugh
Penso 'nome' è in realtà riservato come well.'callee: function() { argomenti: nullo chiamante: nullo Lunghezza: 0 nome: "" ' – Joe