3

Regardless of intent, it's unclear to me why one is valid and other other is not. If someone could point to the relevant part of the language spec, that would be especially helpful.

class Foo {
  bar() {}
}
Foo.prototype.baz = function() {};

const f = new Foo();

new f.baz(); // Valid
new f.bar(); // invalid, throws error
pixatlazaki
  • 572
  • 8
  • 19
  • It's method shorthands in general, nothing to do with static. – ASDFGerte Sep 23 '22 at 18:55
  • Interestingly enough, Babel [compiles it without complaint](https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=MYGwhgzhAEBiD29oG8BQ1oQC5iwS2GgCMwAnACgEoUBfVO1BeAOhIC9oBeaAMwFcAdsHzwBVWgG5UqYKOzQwXaAICmAdziJWZKlNkD5RJao1NtbXaiA&debug=false&forceAllTransforms=true&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env&prettier=false&targets=&version=7.19.2&externalPlugins=&assumptions=%7B%7D)... – Jared Smith Sep 23 '22 at 18:57
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#method_definitions_are_not_constructable – CollinD Sep 23 '22 at 19:16
  • @CollinD That doesn't answer the question, however. MDN attributes shorthand to a shortcut to `bar: function() {}`, but clearly that is not the case here. Otherwise, we wouldn't be getting this error! What's more, in Chrome at least, logging `Foo.bar` gives me `bar() {}` and `Foo.baz` gives me `function() {}`. So there is a difference here... The question is what and why? – kelsny Sep 23 '22 at 19:18
  • I mean, it answers the question but not satisfactorily. MDN docs are fairly authoratative, and indicate that this is the way it is. "Why" might only be answerable by some committee member. I'd imagine there's more information in the ecmascript spec. It would come down to the function not having a `[[Construct]]` on it, but I'm not sure what dictates whether that gets set or not. – CollinD Sep 23 '22 at 19:20
  • I don't think it answers the question at all. I think it makes it more confusing. In the description, it's implying that object method shorthand is a "shortcut" for writing `bar: function() {}`. Again, if that were the case, this error wouldn't happen. – kelsny Sep 23 '22 at 19:21
  • Yeah, I'd say that portion of the docs is most likely incorrect and oversimplifying what's going on. – CollinD Sep 23 '22 at 19:22
  • 2
    Methods don't get an internal `[[Construct]]`, that's how it was decided. It's one detail, where `class` "syntax sugar" doesn't fully align with pre-ES6 syntax. One could theoretically reproduce it with `if (new.target) throw new TypeError(/*...*/)`, but shorthand methods are otherwise afaik by themselves, in generating non-constructor functions, which don't have lexical this (arrow functions cannot be used as constructors either after all) – ASDFGerte Sep 23 '22 at 19:22
  • https://262.ecma-international.org/6.0/#sec-functioncreate might be useful too – CollinD Sep 23 '22 at 19:23
  • 2
    There is no https://tc39.es/ecma262/#sec-makeconstructor in the function creation for methods, and that's probably about it. – ASDFGerte Sep 23 '22 at 19:26

1 Answers1

2

As pointed out in the comments on the original question, the spec defines it this way. A constructor must have the internal [[Construct]]s, and while ordinary function expressions do get this slot set, method definitions do not. This is described in MDN (as another commenter pointed out) as well.

pixatlazaki
  • 572
  • 8
  • 19