Skip to content Skip to sidebar Skip to footer

Javascript - Can We Have Self Invoking Function Modules Assigned To Variables

Can variable be assigned self invoking function module so that the variable reference would trigger calling the function without the () operator. I want the variable to have the la

Solution 1:

This behaviour can be achieved via a getter, using Object.defineProperty, for example:

// Add a `somemethod` property to `window`
Object.defineProperty(window, 'somemethod', {
    get: function() {
        return Math.random();
    }
});
console.log(window.somemethod);
console.log(window.somemethod); // Different value

Solution 2:


Post a Comment for "Javascript - Can We Have Self Invoking Function Modules Assigned To Variables"