Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript function redefinition

Tags:

javascript

Is it possible to redefine a JavaScript function from within its own body. For example, could I do the following?

function never_called_again(args) {
  // Do some stuff
  never_called_again = function (new_args) {
    // Do some new stuff
  }
}

Is the above valid and does it have the correct semantics? I don't want to create a new global variable with the old function name, because I'm trying to do this kind of thing not in the global scope, but from various object scopes, and I don't want name clashes when I redefine the function within those local scopes.

like image 360
David K. Avatar asked Jul 12 '10 09:07

David K.


People also ask

What is redefinition in JavaScript?

The "Redefinition of '{a}'" error is thrown when JSHint or ESLint encounters a variable declaration with an identifier that is the same as that of a built-in native object. In the following example we attempt to declare a variable with the identifier String : var String = "My String"; ​x. 1.

Can you redefine a function in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)

Can we redefine a function?

No, it's not possible.

Can you define a function twice in JavaScript?

functions are data in memory stack, so when you define another function with the same name, it overrides the previous one. Show activity on this post. Well obviously you're not meant to define the same function twice. However, when you do, the latter definition is the only 1 that applies.


2 Answers

It is indeed possible to redefine a function from its body. The technique is used in the so-called Lazy Function Definition Pattern.

It looks like this:

// Lazy Function Definition Pattern
var foo = function() {
    var t = new Date();
    foo = function() {
        return t;
    };
    return foo();
}

This function stores the Date of the first call, and returns this Date afterwards.

If you compare this with the module pattern, the difference is that the initialization only happens when it's first called, not when it's defined. For costly initializations it can be a huge benefit.

// Conditional Module Pattern
var foo = (function() {
    var t;
    return function() {
        if (t) {
            return t;
        }
        t = new Date();
        return t;
    }
})();
like image 189
25 revs, 4 users 83% Avatar answered Oct 06 '22 08:10

25 revs, 4 users 83%


Yes, you can redefine a function that way.

Running This:

function never_called_again(args) {
    console.log('Func 1', args);

    never_called_again = function (new_args, x) {
        console.log('Func 2', new_args, x);
    }
}

never_called_again('arg A', 'arg B');

never_called_again('arg A', 'arg B');

Yields this:

Func 1 arg A
Func 2 arg A arg B
like image 40
Brock Adams Avatar answered Oct 06 '22 09:10

Brock Adams