1

I just read Head First JavaScript book. In the last page, the writers say:

Have you considered that a method is just a property in an object that is set to an anonymous function expression?

Does that mean that the JavaScript interpreter creates an object and assigns an anonymous function to that property?

For example I write this code

function example(){
    // some code goes here
}

But what really happened is the object literal has the example property which I can call just by its property name.

I’m really confused.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Esterlinkof
  • 1,444
  • 3
  • 22
  • 27
  • Or the original duplicate, http://stackoverflow.com/q/336859/438992. – Dave Newton Aug 18 '15 at 00:02
  • @DaveNewton I know his english is bad, but I don't think either of the duplicates actually address the question. I think he's confused about making an object like {} and then setting a function property on it. Something like `var myObject = {}` and then `myObject.myMethod = function() {}` – m0meni Aug 18 '15 at 00:03
  • A [method](http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.27) is a function that is the value of a property. It doesn't matter if that function was created using a function declaration, a named function expression, or an anonymous function expression. – Oriol Aug 18 '15 at 00:03
  • 1
    @AR7 I'm not convinced; the OP's confusion seems to be regarding where the function is defined, e.g., it'd be on `window` in a browser, not an imaginary object literal. I dunno. My point was that if you link to a dupe that itself was marked as a dupe, follow the chain instead of linking to another different dupe. – Dave Newton Aug 18 '15 at 00:06
  • @DaveNewton alright I can definitely see that. – m0meni Aug 18 '15 at 00:08
  • @DaveNewton I chose [this dupe](http://stackoverflow.com/q/1013385/1529630) because it addresses both named and anonymous function expressions, like this question. The [original dupe](http://stackoverflow.com/q/336859/1529630) doesn't (because in fact there is no difference between them, apart from the name). – Oriol Aug 18 '15 at 00:18
  • @Oriol There's a difference, though. The answers in the original dupe seem to address them (in particular, the second and third, regarding scoping, and where the functions end up living). YMMV. – Dave Newton Aug 18 '15 at 00:22

0 Answers0