What is the difference between these two jQuery writing styles?

When I need it:

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

$.test.foo();

And when should I do this?

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

$.testFoo();
+4
source share
1 answer

The following describes an object with two methods

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

$.test.foo();

It just defines two functions

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

$.testFoo();

If you want to associate two functions with each other, use an object. If functions perform two completely different things, then simply define separate functions

+6
source

All Articles