2009-05-21

Some more JavaScript

Some quick tips:

  • To continue a string on the next line in JavaScript, use the \ operator like this:
    var s = "A long text \
    that continues on the next line";


  • Create on the fly functions like this:
    function functionAssembler(operatorSign){
    return new Function("a","b","return a" + operatorSign + "b;");
    }

    Use it to create functions:
    var add = functionAssembler("+");
    var substract = functionAssembler("-");
    var multiply = functionAssembler("*");
    var divide = functionAssembler("/");

    Use the function as always:
    document.write(add(10,5));
    document.write(substract(12,6));
    document.write(multiply(5,3));
    document.write(divide(125,5));

Niciun comentariu: