0

I'm calling makeALine function inside of lines function. And I a trying to save line in makeALine function, using this keyword.

But it says this is undefined. I have no idea why it is undefined. Anyone help?


function makeALine(setOfDots) {
    let line = {
        model: new makerjs.paths.Line(setOfDots)
    }

    console.log(this) //undefined
    this.paths = line;
}

function lines(obj) {
    for (let [key, value] of Object.entries(obj)) {
        console.log('value', value)
        let lineType = key.includes(LINE) ? makeALine(value) : null;

        lineTypes.push(lineType)
    }
    console.log(lineTypes)
}


Sophia Lee
  • 87
  • 8
  • 1
    `makeALine(value)` calls it with no calling context, so `this` is undefined (or the global object). Did you mean for `makeALine` to return an object, or call it with `new`? – CertainPerformance Apr 07 '20 at 00:39
  • In the code you posted, it seems what you want is simply to `return line` from `makeALine(setOfDots) ` so its value will get assigned to `lineType` when calling the function. – Kevin Amiranoff Apr 07 '20 at 01:48
  • @CertainPerformance Ohhh, yes right. I totally forgot ```new``` thanks!! – Sophia Lee Apr 07 '20 at 06:51

0 Answers0