4

Ok, this may sound a bit crazy but hear me out :)

I would like to do the following in javascript:

define START_OF_EVERY_FUNCTION = "try {"
define END_OF_EVERY_FUNCTION   = "} catch () {}"

function TEST () {
    START_OF_EVERY_FUNCTION
    // rest of function
    END_OF_EVERY_FUNCTION
}

Basically, can I define a list of javascript lines (code) and include them as above? I'm looking for a technique versus comments about whether this is a good idea or not or debate over wrapping all functions in a try/catch block.

I know about eval(), but I dont think you can eval statements like the above.

1
  • I think I get what you are trying to do, which is to use pre-written snippets to build new functions, while I'm not sure this is a great approach, I think the best way to accomplish this is to use a server-side language as a sort of "compiler", where you have a small templating language that you use in javascript, just something like {{snippet1}} and then use a server-side language that will go through and replace every instance of {{snippet1}}, for example, with the corresponding code snippet, then you can re-run it anytime anything changes and you have your jscript files updated Commented Sep 7, 2010 at 9:00

6 Answers 6

4

This might be goofy but you could define a master function and run other functions through it by passing them in.

var execute = function(func){
    alert('before');
    func();
    alert('after');
};

function sayHi(){
    alert('hi there');
}

execute(sayHi);

As requested, an example with passing arguments.

var execute = function(func){
    alert('before');
    var ret = func.apply(null, Array.prototype.slice.call(arguments, 1));
    alert('after');
};

function saySomething(sayWhat){
    alert(sayWhat);
}

execute(saySomething,'hey there');
Sign up to request clarification or add additional context in comments.

3 Comments

This might be more like the "Javascript way".
Can you provide an example that passes arguments to the function?
@Asaph: Consider the arguments array that is part of a called function developer.mozilla.org/En/Core_JavaScript_1.5_Reference/…
1

That is not allowed in JavaScript.

2 Comments

Yes, clearly - but is there a technique that can achieve that goal?
As far as I know, no. It looks like you are trying to simulate a macro, and I believe macros are used with complier languages, not interpreted.
1

You could extend the Function prototype:

Function.prototype.tryThis = function() {
    try {
        this();
    }catch(ex){
        alert('Caught '+ex);
    };
};

function tryIt() {
    alert('Inside tryIt');throw "My Error from tryIt";
}

tryIt.tryThis();

Comments

0

You need to look into aspect oriented programming for JavaScript. You can create hooks for function entry and exit. Tools like JSUnit do this for example.

Comments

0

I think you can do this with the "new Function" operator. I've never used it myself, since I'm not clinically insane, but I believe you can pass it a string which it will evaluate and use as the function body. You can also get the code for each function by calling myFunction.toString(). So put together, it'd be something like this:

var functionsToMessUp = ['myFunc1', 'myFunc2'];

for (var i = 0; i < functionsToMessUp.length; ++i) {
    var theFunc = window[functionsToMessUp[i]]; // assuming they're in global scope
    window[functionsToMessUp[i]] = new Function(
        START_OF_EVERY_FUNCTION
        + theFunc.toString()
        + END_OF_EVERY_FUNCTION
    );
}

Now, the above almost certainly won't work - there's parameters and other things to take into consideration, and I don't even think that's how the new Function constructor works, but if you really want to go down this path (which I really don't recommend), then this might be a good starting point for you.

Comments

0

Maybe something like this?

function tryCatch(callback) {
  try {
    callback();
  } catch() {}
}

var myFunction = function() {
  // do some stuff
};

tryCatch(myFunction);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.