0

In the code below why

alert(window.concatenateAandB());

works fine and displays "Hello world"? I know based on the tip from http://java.dzone.com/articles/javascript-java-developers this refers to current object which is "window" but again I dont get it! I am from Java background and thought the right version would have to be

window.Simple.concatenateAandB()

can anyone helped me out here?

function Simple(a, b) {
  var propertyA = a;
  var propertyB = b;

  this.concatenateAandB = function() {
    return propertyA + propertyB;
  }

}

Simple("Hello ","world")

alert(window.concatenateAandB());  
C graphics
  • 7,308
  • 19
  • 83
  • 134
  • The first time you call `Simple`, you create a method of `this`, which is `window` in non strict mode. Then `window.concatenateAandB` will be that function. – Oriol Jun 20 '14 at 01:39
  • Regarding how `window.concatenateAandB()` is able to alert `"Hello world"`, see [**How do JavaScript closures work?**](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) Note: Closures are independent of `this`. Your snippet just uses them together. – Jonathan Lonowski Jun 20 '14 at 01:49

0 Answers0