Home > Enterprise >  Javascript. What is the functional equivalent for class constructors?
Javascript. What is the functional equivalent for class constructors?

Time:12-22

Class constructor initializes and create objects/functions in a class. If I'm using functions, how would I initialize a function within the function?

This is the simple class

export default class MainProcess{
    constructor() {
                 this.isReady = false
        this.init()
         

    }
    init() {
        this.setupApplicationMenu()
        this.initWindowManager()
        this.getIcons()
    }

}

how can I initiate a MainPRocess Function?

CodePudding user response:

While I'm not entirely sure I understand the question, I think you're asking "How can I create a function in a way that's similar to how I'm used to writing classes, but without using the class keyword?" Here's an example of that:

function Example () {
  this.value = 10;

  // instance method
  this.print = function () {
    console.log(this.value);
  }
}

// static method
Example.printHello = function () {
  console.log('hello world');
}

const example1 = new Example();
example1.print(); // 10
example1.value = 20;
example1.print(); //20

console.log(Object.getPrototypeOf(example1).constructor.name); // "Example"

const example2 = new Example();
example2. print(); //10

Example.printHello(); // "hello world"

CodePudding user response:

Functions are part of the class. Classes are a grouping of functions(methods) and data(as properties). These functions are used to modify properties.

In the above example, you created a class MainProcess with some functions. However, functions defined in the init method is not present. The compiler will throw an error.

constructor is a special method used to create an object with that class.

If I'm using functions, how would I initialize a function within the function?

It seems you are mixing two concepts function constructors in JS and Classes which are introduced later. Class is nothing, but a syntactic sugar on function constructor. JS is a prototype-based language.

difference b/w function and function constructor?

Functions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function constructor was created. This is different from using Global_Objects/eval with code for a function expression.

var x = 10;

function createFunction1() {
    var x = 20;
    return new Function('return x;'); // this |x| refers global |x|
}

function createFunction2() {
    var x = 20;
    function f() {
        return x; // this |x| refers local |x| above
    }
    return f;
}

var f1 = createFunction1();
console.log(f1());          // 10
var f2 = createFunction2();
console.log(f2());          // 20

I highly recommend you first understand the how JS has implemented class in it.

  • Related