Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Function Format: Is one of these a method?

Tags:

javascript

What's the difference between:

pause: function () {
    this.state.gamePaused = true;
},

and

function pause() {
    this.state.gamePaused = true;
}
like image 278
user1822824 Avatar asked Mar 19 '26 21:03

user1822824


1 Answers

The first example is probably part of an object, which means that pause is a function of an Javascript object and the second one pause as a stand alone function.

I'd expect to see the first example in something like:

var someObject = {
  pause: function () {
    this.state.gamePaused = true;
  },
  anotherProperty : "some value"
}

And thus it is used like:

someObject.pause();

On the other hand, the second example would be used like:

pause();
like image 125
Ivan Avatar answered Mar 22 '26 10:03

Ivan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!