0

After reading a lot, I still don't quite understand the term function objects in JS. From what I have read, following are my interpretations. Please correct if I'm wrong.

function myfunc(x){
 return x;
}

Interpretation: myfunc is a function object auto created by JS interpreter internally whenever a new function like myfunc is declared as shown above.

var myObj = new myfunc();
var mynewObj = new myfunc();

Interpretation: myObj and mynewObj are instances(objects) of myfunc thus can be said to be a function objects

Are my interpretations correct. Any other examples of function objects?

thanks

bt

b Tech
  • 412
  • 4
  • 14

1 Answers1

2

Interpretation: myfunc is a function object auto created by JS interpreter internally whenever a new function like myfunc is declared as shown above.

Almost, but not quite. myfunc is an identifer that refers to (points to) a function created by the JavaScript engine.1 That function is an object, because all functions are objects in JavaScript. You could call it a "function object" if you like; most people would just call it a "function." (In JavaScript "function" and "function object" are synonyms.)

var myObj = new myfunc();
var mynewObj = new myfunc();

Interpretation: myObj and mynewObj are instances(objects) of myfunc thus can be said to be a function objects

No, they're not function objects. They're just objects. They are indeed instanceof myfunc which means that the object myfunc.prototype points to is in their prototype chain, but it's not correct at all to call them "function objects" because they aren't functions.


1 "JavaScript engine" - This is the term I use in preference to "JavaScript interpreter" because any modern JavaScript engine is a just-in-time-compiler plus runtime environment, not an interpreter. It's a subtle distinction.


In a comment, Jamie Dixon mentions the Function function. It's just a function that creates functions based on source code strings. There are virtually no use cases for it in modern JavaScript (just as there are virtually no use cases for eval in modern JavaScript).

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • There's also a Function object (capital F) that can be used in place of the function value declaration i.e. `var foo = new Function(args, body);`. MSDN (heh) refers to these as function value (function) and Function object. – Jamie Dixon Oct 03 '16 at 09:58
  • 1
    @JamieDixon: Yeah, well, MSDN, nuff said. I intentionally didn't bring `Function` into the mix here to avoid confusion. – T.J. Crowder Oct 03 '16 at 10:00
  • @TJ Crowder, if `myfunc` can also be referred as `function objects` that means that object has been created i.e it's already been instantiated and suppose `myfunc` is definition is: `function myfunc() { return this.x ="Great fun"; } Then `myfunc.x;` should output `Great fun` but the output is `undefined`. The output means the object `myfunc` has not been instantiated. – b Tech Oct 03 '16 at 10:25
  • @bTech: *"suppose myfunc is definition is: `function myfunc() { return this.x ="Great fun"; }` Then `myfunc.x;` should output `Great fun`"* No, it shouldn't. `this` inside `myfunc` is not (normally) a reference to the function itself. If you use `new myfunc()`, `this` will refer to a new object (which isn't a function). If you do `myfunc()` (no `new`), in strict mode `this` will be `undefined` and in loose mode `this` will be the global object, but either way it's not the same as `myfunc` so `this.x = 'Great fun'` does not create a property on `myfunc`. – T.J. Crowder Oct 03 '16 at 10:29
  • so `this` inside `myfunc` will be `undefined`? (JS default for variables that has not been assigned)` and does each instantiated object has it's own private 'this' variable that points to itself? – b Tech Oct 03 '16 at 10:37
  • @bTech: *"so `this` inside `myfunc` will be `undefined`?"* Not necessarily, read my comment again. See also: [*How does the `this` keyword work?*](http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work?rq=1) – T.J. Crowder Oct 03 '16 at 10:39
  • @TJ Crowder: all constructor function's (actually all function's) `function.prototype` points to `Object {}` (an object) which means they inherit prop/methods from `Object{}` so is this the reason all functions are said to be objects in JS? – b Tech Oct 03 '16 at 13:47
  • @bTech: 1. No. Functions are objects because that's how the language is defined, it has nothing to do with their `prototype` property. The `prototype` property on the function can be anything the author of the function wants it to be (including `null`). If it's an object, and if you use `new` with the function, the object that gets created by `new` will use the object `prototype` referred to as its `[[Prototype]]` (the object it inherits things from), but that's completely unrelated to functions being objects. – T.J. Crowder Oct 03 '16 at 13:54