What the difference between those two?

javascript

Solution

The first snippet takes `this` object, whatever it is, and assigns a function to its slot (field) named `method1`. `this` can represent different objects, depending upon how `test1` is called:

- when called as a standalone function -- `test1()` -- `this` will be `window`

- when called as a constructor -- `new test1()` -- `this` refers to the object being created

- when called via `call` or `apply` -- `test1.apply(someObject)` -- `this` refers to the argument

The second snippet takes the object `test2` and assigns a function to its slot named `method1`.

Problem

Is there any difference between these two: ``` var test1 = function () { this.method1 = function() {} } ``` and ``` var test2 = function() {}; test2.method1 = function() {}; ```

Original source