Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you alter a Javascript function after declaring it?

Let's say I have var a = function() { return 1; }. Is it possible to alter a so that a() returns 2? Perhaps by editing a property of the a object, since every function is an object?

Update: Wow, thanks for all the responses. However, I'm afraid I wasn't looking to simply reassign a variable but actually edit an existing function. I am thinking along the lines of how you can combine partial functions in Scala to create a new PartialFunction. I am interested in writing something similar in Javascript and was thinking that the existing function could perhaps be updated, rather than creating an entirely new Function object.

like image 747
pr1001 Avatar asked Jan 25 '10 23:01

pr1001


People also ask

Can you reassign this in JavaScript?

No, it's not possible. You can call a method with a specified value for this (using method. apply() / method. call() ) but you cannot re-assign the keyword, this .

Can a declared function be called before it has been declared in the script?

So generally html - including javascript - has a top down method of parsing code. However, functions go out of this route as they are read first, but only the function head (= first line). That is why function calls are possible before declaration.

What happens if you pass an extra parameter to a method in JavaScript?

Unless otherwise specified in the description of a particular function, if a function or constructor described in this clause is given more arguments than the function is specified to allow, the extra arguments are evaluated by the call and then ignored by the function.

Can you declare a function inside a function in JavaScript?

A function is called “nested” when it is created inside another function. It is easily possible to do this with JavaScript.


1 Answers

You can do all kinds of fun stuff with javascript, including redefining functions:

let a = function() { return 1; } console.log(a()); // 1      // keep a reference let old = a;     // redefine a = function() {   // call the original function with any arguments specified, storing the result   const originalResult = old.apply(old, arguments);   // add one   return originalResult + 1; };  console.log(a()); // 2 

Voila.

Edit: Updated to show this in a crazier scenario:

let test = new String("123"); console.log(test.toString()); // logs 123 console.log(test.substring(0)); // logs 123 String.prototype.substring = function(){ return "hahanope"; } console.log(test.substring(0)); // logs hahanope 

You can see here that even though "test" is defined first, and we redefine substring() afterwards, the change still applies.

Side note: you really should reconsider your architecture if you're doing this...you're going to confuse the crap out of some poor developer 5 years down the road when s/he's looking at a function definition that's supposed to return 1, but seems to always return 2....

like image 172
jvenema Avatar answered Sep 20 '22 18:09

jvenema