1
class C { foo() {} }
C.prototype.foo // function foo() {}
C.prototype.foo.prototype // undefined - why?

Why is the .prototype property of class methods not set when created via the class method syntax?

10
  • What do you expect that to be? Commented May 22, 2017 at 15:46
  • Because JavaScript attempts to hide/cover-up the power of prototypal inheritance while not providing true classical inheritance with the class keyword. It's the worst of both worlds, tbh. Commented May 22, 2017 at 15:47
  • 1
    I expect it to be an object per a normal function declaration (I think). Commented May 22, 2017 at 15:47
  • 1
    Only constructor functions (and classes) have prototypes. Commented May 22, 2017 at 15:49
  • 2
    @vabii Just ran it in Chrome console, getting undefined. Commented May 22, 2017 at 15:59

1 Answer 1

3

Because methods (like arrow functions) are no constructors, and don't need a .prototype from which the prototype of instances would be initialised, none will get created.

This is a new feature in ES6, which distinguishes method definitions in object literals and class definitions from usual function definitions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.