1

Constructor:

function Team (type) {
  this.type = type;
}

//this will output this empty object inherited from Object.property
console.log(Team.prototype);
-> Team {}

//this one outputs nothing in my console
console.log(Object.getPrototypeOf(Team));
//is it inheriting from this one, the one for all functions?
-> Function.prototype //??

What is the difference between the .prototype property and Object.getPrototypeOf? What's else does Function.prototype (the one that all functions and constructors inherit from) prototype do, except for storing properties?

Svetan Dimoff
  • 519
  • 6
  • 24
  • Is the question "why does `Team` have a property `getPrototypeOf`"? – Ruan Mendes Mar 12 '15 at 21:49
  • Question is what's the difference between `Team.prototype` and `Team.getPrototypeOf`. – Svetan Dimoff Mar 12 '15 at 21:51
  • 1
    I think you want `console.log(Object.getPrototypeOf(Team))` to view the objects that all functions inherit from. – Bergi Mar 12 '15 at 22:08
  • @Bergi - Right, it's my bad. Still - it outputs an empty function, what is this for, where does it come to play? – Svetan Dimoff Mar 12 '15 at 22:10
  • 1
    @StevensHaen `Object.getPrototypeOf()` returns the object that an instance is actively inheriting from. The `prototype` property of a function is what `new` instances of that function will inherit from. So, `var t = new Team(); console.log(Object.getPrototypeOf(t) === Team.prototype);`. Constructors, being `new Function`s themselves, inherit from `Function.prototype`. – Jonathan Lonowski Mar 12 '15 at 22:12
  • 1
    @StevensHaen: Uh, that is because `Function.prototype = function(){}` (for dubious reasons, but [it's what the spec says](http://es5.github.io/#x15.3.4)). That function object does have the expected properties like `call`, `apply`, `bind` etc. – Bergi Mar 12 '15 at 22:13
  • @StevensHaen: Can you please [edit] your post to only ask specifically about the parts that you don't understand, and that are not answered by [\_\_proto\_\_ VS. prototype in JavaScript](http://stackoverflow.com/q/9959727/1048572)? – Bergi Mar 12 '15 at 22:21
  • @JonathanLonowski You explained it great, got it now. Could you please give me an example of `getPrototypeOf()`? – Svetan Dimoff Mar 12 '15 at 22:24

1 Answers1

2

Team is a function, so it inherits all the properties from Function.prototype. Function is also an (inherits from) object, so it has all the properties from Object.prototype. However, Object.getPrototypeOf is a "static" method on Object, so it's no inherited.

Object.getPrototypeOf(Team) points to the same object as Function.prototype. Team.getPrototypeOf is undefined.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • People say functons in JS are objects, but they're not, since you cannot check their properties. Their instances are. Why does `Team.getPrototypeOf` result in `undefined` in my console? – Svetan Dimoff Mar 12 '15 at 21:53
  • Yes functions are objects. `(function(){}) instanceof Object` outuputs true. And yes, you can check its properties. `var a = function(){}; a.b = 1; console.log(a.b)` outputs 1. You need to clarify your question. – Ruan Mendes Mar 12 '15 at 21:54
  • For example you cannot access a property from the function directly (`function Team (type) {this.type = type;}`. `Team.type()` doesn't work), but an instance of it rather - (`var team = Team();`, `team.type()` works). – Svetan Dimoff Mar 12 '15 at 22:05
  • You probably mean `Object.getPrototypeOf(Team) == Function.prototype` with your last sentence – Bergi Mar 12 '15 at 22:07
  • 2
    `Team.prototype !== Function.prototype`. `Team.prototype` is brand new Object, roughly like if you have written `Team.prototype = {}`. – Frax Mar 12 '15 at 22:13
  • @Stevens: your code modifies `window`/`global` objects instead of new instance (and breaks on `team` being undefined). You should use `new Team()` instead of just `Team()`. – Frax Mar 12 '15 at 22:19
  • @Bergi Yes, that is I what meant – Ruan Mendes Mar 13 '15 at 00:47
  • @Frax I stand corrected – Ruan Mendes Mar 13 '15 at 00:48