1

how do I generate something like:

function generator() {
static funct(){}
}

I think I'm wrong, but from what I understand the class {} method is built on top of function(){}, so it should have the same method. The point is that static methods don't have a .prototype, and I'd like to know how they're created via class {}

@UPDATE
apparently from what I understand the class {} method actually builds static functions through Object(), since both can create static functions.

class A {
  static a() {}
}

const B = {b(){}};

console.log('a' in A && 'b' in B)
console.log(`${A.a.prototype}, ${B.b.prototype}`);
  • 1
    `function generator(){}; generator.funct = function(){};` – ASDFGerte Dec 30 '21 at 01:47
  • @ASDFGerte it's generates a ``.prototype``, so it's not a static function –  Dec 30 '21 at 01:54
  • If you care about all little details, i am not sure, if it's even possible. To start with, you'd need a function, that has no `[[Construct]]`, but `non-lexical-this`. Too late here to investigate, but it sounds difficult to get. Afaik, `class` is mostly syntactic sugar, but in you want to go into details, "mostly" doesn't mean "can be absolutely fully expressed in ES5". – ASDFGerte Dec 30 '21 at 02:31
  • 2
    What are you trying to accomplish with this? – Derek Dec 30 '21 at 03:18
  • @RafaeldeSouza It is a static method. It has a `.prototype` property, so what? That doesn't make it a non-function. If you want to create methods that don't have a `.prototype` property without `class` syntax, use a method definition in an object literal `({funct(){…}}).funct`. – Bergi Dec 30 '21 at 05:59

1 Answers1

0

Honestly, the closest you can get is just removing it yourself:

function generator() {
  if (!(this instanceof TestClass)) throw new TypeError("Cannot call a class as a function");
}

generator.funct = function() {
  console.log(this.funct);  
  return "test";
}

generator.funct.prototype = undefined;

console.log(generator.funct())
console.log(generator.funct.prototoype)

gives the same output as

class generator {
  static funct() {
    console.log(this.funct)
    return "test";
  }
}

console.log(generator.funct())
console.log(generator.funct.prototoype)

But I don't see why this is necessary, even in Babel when transpiling into ES5 syntax, static methods still have prototypes.

skara9
  • 4,042
  • 1
  • 6
  • 21