0

I am trying to understand the behaviour of the new keyword and call in ES5. Let's suppose someone makes a mistake, has this code and runs it in a browser:

function Shape(colour) {
    this.colour = colour;
}

function Circle(colour) {
    Shape(this, colour);
}

var shape1 = Shape('blue'); // undefined
var shape2 = Circle('red'); // undefined

My question is, what happens?

window.colour // no points to window; shouldn't this point to 'red' now?

// window.colour now points to a circle object; why is there no colour property on the 
//object but it rather points to a circle object?
new Circle('red'); 

I have read a few guides on this and new but haven't figured how they influence what happens.

Thanks for your help, Anagni

Liam
  • 22,818
  • 25
  • 93
  • 157
Sebastian
  • 377
  • 3
  • 11
  • Your `Shape(this, colour)` is calling `Shape` with `window` and the passed `colour`, but `Shape` only accepts one parameter, not two. – CertainPerformance Oct 08 '18 at 15:28
  • I have written about how `this` gets bound here: https://stackoverflow.com/questions/41496958/this-does-not-work-properly-in-another-event-im-clueless-as-to-why/41496969#41496969 – Scott Marcus Oct 08 '18 at 15:29
  • @CertainPerformance Right; stupid oversight. Thanks! – Sebastian Oct 09 '18 at 07:24

0 Answers0