admin管理员组文章数量:1025493
I created a class with a method:
class MyClass{
myMethod(){
return ...;
}}
After that I push every instance of that class to an array:
let myList = [];
myList.push(myClass)
How can I call myMethod() within a loop? This code fails:
for (var i = 0; myList.length; i++) {
myList[i].myMethod();
}
Uncaught TypeError: Cannot read property 'myMethod' of undefined
Thx, piccus
I created a class with a method:
class MyClass{
myMethod(){
return ...;
}}
After that I push every instance of that class to an array:
let myList = [];
myList.push(myClass)
How can I call myMethod() within a loop? This code fails:
for (var i = 0; myList.length; i++) {
myList[i].myMethod();
}
Uncaught TypeError: Cannot read property 'myMethod' of undefined
Thx, piccus
Share Improve this question asked May 24, 2017 at 6:04 piccuspiccus 1712 silver badges9 bronze badges 02 Answers
Reset to default 2You should crate an instance with new
operator in order to call a method:
class myClass{
myMethod(){
console.log('hi');
}
}
let myList = [];
myList.push(new myClass())
myList.push(new myClass())
myList.push(new myClass())
myList.push(new myClass())
myList.push(new myClass())
for (var i = 0; i < myList.length; i++) {
myList[i].myMethod();
}
If you need to call this method without instance, define it as static
:
class myClass{
static myMethod(){ // notice static
console.log('hi');
}
}
let myList = [];
myList.push(myClass)
myList.push(myClass)
myList.push(myClass)
myList.push(myClass)
myList.push(myClass)
for (var i = 0; i < myList.length; i++) {
myList[i].myMethod();
}
You could keep track of each instance of MyClass
by pushing this
into myList
when the constructor is called. That way you don't have to keep remembering to push into myList
each time you construct a new MyClass
instance.
var myList = [];
function MyClass (theAnswer) {
this.theAnswer = theAnswer;
this.getTheAnswer = function () {
console.log(this.theAnswer);
};
// Keep track of all instances of MyClass here.
myList.push(this);
}
new MyClass(42);
new MyClass(null);
new MyClass('Dunno');
for (var i = 0; i < myList.length; i++) {
myList[i].getTheAnswer();
}
I created a class with a method:
class MyClass{
myMethod(){
return ...;
}}
After that I push every instance of that class to an array:
let myList = [];
myList.push(myClass)
How can I call myMethod() within a loop? This code fails:
for (var i = 0; myList.length; i++) {
myList[i].myMethod();
}
Uncaught TypeError: Cannot read property 'myMethod' of undefined
Thx, piccus
I created a class with a method:
class MyClass{
myMethod(){
return ...;
}}
After that I push every instance of that class to an array:
let myList = [];
myList.push(myClass)
How can I call myMethod() within a loop? This code fails:
for (var i = 0; myList.length; i++) {
myList[i].myMethod();
}
Uncaught TypeError: Cannot read property 'myMethod' of undefined
Thx, piccus
Share Improve this question asked May 24, 2017 at 6:04 piccuspiccus 1712 silver badges9 bronze badges 02 Answers
Reset to default 2You should crate an instance with new
operator in order to call a method:
class myClass{
myMethod(){
console.log('hi');
}
}
let myList = [];
myList.push(new myClass())
myList.push(new myClass())
myList.push(new myClass())
myList.push(new myClass())
myList.push(new myClass())
for (var i = 0; i < myList.length; i++) {
myList[i].myMethod();
}
If you need to call this method without instance, define it as static
:
class myClass{
static myMethod(){ // notice static
console.log('hi');
}
}
let myList = [];
myList.push(myClass)
myList.push(myClass)
myList.push(myClass)
myList.push(myClass)
myList.push(myClass)
for (var i = 0; i < myList.length; i++) {
myList[i].myMethod();
}
You could keep track of each instance of MyClass
by pushing this
into myList
when the constructor is called. That way you don't have to keep remembering to push into myList
each time you construct a new MyClass
instance.
var myList = [];
function MyClass (theAnswer) {
this.theAnswer = theAnswer;
this.getTheAnswer = function () {
console.log(this.theAnswer);
};
// Keep track of all instances of MyClass here.
myList.push(this);
}
new MyClass(42);
new MyClass(null);
new MyClass('Dunno');
for (var i = 0; i < myList.length; i++) {
myList[i].getTheAnswer();
}
本文标签: loopsJavascript Call method on object in array of objectsStack Overflow
版权声明:本文标题:loops - Javascript: Call method on object in array of objects - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745636031a2160450.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论