Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
How do you represent a class as a function in JavaScript

Per Classes in MDN

Classes are in fact "special functions"

How would something like

class Foo {
  constructor(x) {
    this.x = x;
  }

  foo() {}

}

be translated to just function (ignoring the "hoisting" feature)

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

// declare the "class"
function Foo (x) {
  this.x = x;
}

// declare the instance method
Foo.prototype.foo = function () {
  return 'i am foo';
}

// create an instance
const f = new Foo(42);

// invoke its methods
console.log(f.foo()); // 'i am foo'

// access its properties
console.log(f.x); // 42

about 4 years ago · Juan Pablo Isaza Report

0

Non hoisted, (as much as constructor functions based on class syntax are not hoisted,) due to an immediately invoked function expression (IIFE) which serves as module pattern ...

const Foo = (function () {    //  class Foo {
                              //
  function Foo(x) {           //    constructor(x) {
    this.x = x;               //      this.x = x;
  }                           //    }
  function foo() {            //    foo() {
  }                           //
  Foo.prototype.foo = foo;    //    }
                              //
  return Foo;                 //
                              //
}());                         //  }
about 4 years ago · Juan Pablo Isaza Report

0

Well really everything is objects - a class is simply a blueprint for an object that has some methods and some values. It's all objects with features that can either store values or transform values.

A class is something - a function does something. Calling it a special function is if anything unnecessary at best and confusing at worst.

Basically - don't worry about it.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!