Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a line into a function in JavaScript

In JavaScript, is it possible to insert a line into a function that already exists? I want to create a function that inserts a line at a specific position in a function:

function insertLine(theFunction, lineToInsert, positionToInsert){
    //insert a line into the function after the specified line number       
}

For example, would it be possible to programmatically insert the line checkParameterTypes(min, "string", max, "string"); before the first line of this function?

function getRandomInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
like image 658
Anderson Green Avatar asked Apr 25 '13 03:04

Anderson Green


2 Answers

If you want something to happen at the beginning of a function, you can use the following. You do have access to this and the arguments from your injected function. So it will still work for functions that require a specific context.

function inject(before, fn) {
    return function(){
        before.apply(this, arguments);
        return fn.apply (this, arguments);
    }
}

For example

function add(a, b) {
   return a + b; 
}    

function concat(a, b) {
    return a + b;
}

/**
 * You can repeat index and type to check multiple arguments
 */
function createArgumentChecker(index, type /**index, type, ... */) {
    var originalArgs = arguments; 
    return function() {
        for (var i=0; i < originalArgs.length; i+=2) {
             var index = originalArgs[i],
                 requestedType = originalArgs[i+1],
                 actualType = typeof arguments[index];
             if (typeAtIndex  != actualType) {
                 console.log("Invalid argument passed at index " + index  + 
                     ". Expected type " + requestedType +  "but it's " + actualType );
             }
        }
    }
}

function logArguments() {
    console.log(this, arguments);
}

// Inject an argument checker
add = inject(add, createArgumentChecker(0,"number", 1, "number"));
concat = inject (concat, createArgumentChecker(0, "string", 1, "string"));

// You can even do it multiple times, inject an argument logger;
add = inject(add, logArguments);
concat = inject(concat, logArguments);

JSfiddle

This can be handy when debugging websites that you can't modify the source code, I wouldn't use it do parameter checking unless you can strip it our for the production version.

like image 90
Juan Mendes Avatar answered Oct 21 '22 16:10

Juan Mendes


Yes you can but using eval is always evil ;)

function insertInbetween (arr, value, index) {
  var inserted, i, newarr = [];
  for (i = 0; i < arr.length; i++) {
    if(i == index && !inserted) {
      newarr[i] = value;
      inserted = true;
    }
    newarr.push(arr[i]);
  }
  return newarr;
}

function test (a, b) {
    console.log(a,b);
}

var fstrarr = test.toString().split('\n');
eval(insertInbetween(fstrarr, "console.log('injected!');", 1).join('\n'));

Edit: As mentioned in the comments to your question you'll loose scope by doing so.

like image 2
LJᛃ Avatar answered Oct 21 '22 15:10

LJᛃ