PrepAway - Latest Free Exam Questions & Answers

5 Comments on “Which code should you use?

  1. rik says:

    Seems to me you are both right.
    Using ‘this’ inside a function that is used as a parameter for a function will make said ‘this’ point to the window object.
    As a workaround, the ‘this’ can be assigned to a variable (‘that’) which is then used as a parameter. So, if “var that = this;” was added to the square() function it should work like so:

    function square(side){
    this.side = side;
    var that = this;
    this.area = calcArea(that);
    }
    function calcArea(obj){
    return obj.side * obj.side;
    }

    So the way I see it, either answers A, B, C and D are all wrong or the question is incomplete.




    0



    0
  2. stijn says:

    answer D is correct

    function square(side) {
    this.side = side;
    this.area = calcArea;
    }

    function calcArea() {
    return this.side * this.side;
    }

    var square = new square(10);
    console.log(square.area()); //result 100




    1



    0

Leave a Reply