Mark Boulder
Mark Boulder

Reputation: 14287

What's the difference between these two styles of writing jQuery?

When should I do this:

$.test = {
  foo: function() {
    this.bar();
  },
  bar: function() {
  }
}

$.test.foo();

And when should I do this?

$.testFoo = function() {
  $.testBar();
}
$.testBar = function() {
}

$.testFoo();

Upvotes: 3

Views: 70

Answers (1)

user4639281
user4639281

Reputation:

The following defines an object with two methods

$.test = {
  foo: function() {
    this.bar();
  },
  bar: function() {
  }
}

$.test.foo();

This just defines two functions

$.testFoo = function() {
  $.testBar();
}
$.testBar = function() {
}

$.testFoo();

If you want to associate the two functions with each other then use an object. If the functions do two completely different things then just define separate functions

Upvotes: 6

Related Questions