1

I need to create object in JavaScript but during object initialization some field of object should be evaluated.

For example in Python I could do this:

class A:
    def __init__(self, a):
        self.b = []
        for i in range(6):
            self.b.append(a * i)

a = A(3)
print(a.b)  # [0, 3, 6, 9, 12, 15]

But when I tried similar code in JavaScript, it doesn't work

function A (a){
    this.b = []; 
    for (var i = 0; i < 5; i++){
        this.b.push(a * i);
    }
}

a = A(3);
console.log(a.b); // no results

https://jsfiddle.net/jf6txec1/

How could I perform some evaluation during object initialization in JavaScript?

Aleks Lee
  • 136
  • 1
  • 1
  • 10

0 Answers0