What is the static keyword equivalent for use in function (without Class syntax)?

class Class1 {
  static a = 10;

console.log(Class1.a); // output 10

function test() {
  static temp = 10; // error

Is there a keyword that can be used in a function that will make temp accessible like a static property in a Class?

Thank you!

No, there is no other keyword you can use to get the functional equivalent of a static member of a class. But there may be some ways to mimic it depending on what you are trying to accomplish. Can you give us an example of what you are trying to do? Put it in some context for us?

i am trying to understanding the difference between Class and Function in JS. From some of the codes I have seen, it seems that function can return a Class(?!). Trying to wrap my head around this.

class SomeClass {

  constructor() {
    this.myMethod = () => {

  static staticMethod() {

function returnClass() {
  return SomeClass;

const returnedClass = returnClass(); // returnClass() returned a Class (!?)

(new returnedClass()).myMethod(); // No error, valid output "myMethod"
returnedClass.staticMethod(); // No error, valid output "staticMethod"

Classes in JS, like any other language, need to be instantiated: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Classes in JS don’t have a “static” equivalent as in languages like Java or C#/C++. It’s just not supported.

I think the question you want to ask instead is about what is prototypal based programming in Javascript. class keyword is just a syntactic sugar.

In fact, the typeof(SomeClass) is actually a function.

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects.

By default all methods declared inside the class will go to the prototype. static keyword changes this default behaviour and writes the method directly on the class. So:

class Class1 {
  static a = 10;

// is equivalent of

function Class2() {}
Class2.a = 10;

console.log(Class1.a); // 10
console.log(Class2.a); // 10

Hope this will help a bit :slight_smile:

Can a function actually return a Class?

When my mouse hover of the model method in mongoose, I have this pop-up in VS Code:

function model<Document>(name: string, schema?: Schema<any>, collection?: string, skipInit?: boolean): Model<Document, {}> (+1 overload)

Thanks to DanCouper from another post, I get to know that this is actually from TypeScript.

Reading up on TS, I get to understand that the mongoose.model() return a Model type.

Question 1: Is the return type a Model Object or a Model Class?

const carModel = mongoose.model('car',mySchema);

carModel.find(); // this works. Since find() is a static method, it implies carModel is a Class. 

So a function can return a Class?

Question 2:

console.log(typeof carModel); // output function

However, the return type as indicated by the method pop-up is :Model<Document, {}>. If it is a function that is returned, it should:

function model<Document>(name: string, schema?: Schema<any>, collection?: string, skipInit?: boolean): () => { }

Ref: https://mongoosejs.com/docs/api/model.html

Thank you all… I have learnt much from your replies.

I think you wanted to ask can function implement (have the same capabilities) class, right? Because any function can return any value or struct existing in JavaScript:

function classFactory(title) {
  return class Person {
    constructor(name) {
      this.name = `${title} ${name}`;

    greet() {

const MrPerson = classFactory('Mr.');
const me = new MrPerson('Snigo');
me.greet(); // Mr. Snigo