function Foo(){
this.conceal = function(item) {
console.log(item); // "a" "b" "c"
};
};
function toggleView(foos) {
var myFoo = new Foo(); // Create new instance (here or outside this fn)
for(a in foos){
myFoo.conceal(foos[a]); // use your instance and pass as items arg. reference
}
}
toggleView(["a","b","c"]);
Beispiel macht toggleView
ein Prototyp von Foo
function Foo(){
this.conceal = function(item) {
console.log(item); // "a" "b" "c"
};
}
Foo.prototype.toggleView = function(foos) {
for(a in foos){
this.conceal(foos[a]); // invoke .conceal Method using `this` reference
}
}
var myFoo = new Foo(); // Create new instance
myFoo.toggleView(["a","b","c"]);
ist es hier nicht genug Code, um zu sehen, was falsch ist. Wenn 'foos' wirklich ein Array ist, sollten Sie eine einfache 'for'-Schleife mit einem numerischen Index oder sonst' .forEach() 'verwenden, um zu iterieren. Aber was du hast, sollte funktionieren, es sei denn, 'foos' ist * nicht * was du sagst. – Pointy
Die Schleife besucht eine Eigenschaft, die keine Instanz von 'Foo' enthält. Beachten Sie, dass 'for..in'-Schleifen nicht streng auf Indizes beschränkt sind. Related: [Warum ist "für ... in" mit Array-Iteration eine schlechte Idee?] (Https://stackoverflow.com/questions/500504/why-isusing-for-in-in-array-iteration-a- bad-idea) –
Bist du sicher, dass du 'new' benutzt, um Foo Instanzen zu erstellen? – elmasse