Raymond
Raymond

Reputation: 572

What is the difference between function declarations and function expressions inside an object literal?

I'm new in JavaScript.

I want to what's the difference between this

function aa() {
  // code
}

function bb() {
  // code
}

and this

var b = {
  aa: function () {
    //code
  },
  bb: function () {
    //code
  }
};

I know about the first, but I don't know about the other one.

What's it called and what's differences?

Upvotes: 0

Views: 48

Answers (1)

Blender
Blender

Reputation: 298046

The first one creates two named functions: aa and bb.

The second one creates an object called b that has two properties: aa and bb, both of which have anonymous functions as values.

They do different things, so you can't really say which one is "better".

Upvotes: 1

Related Questions