jsMode: Simple javascript code that works in Browser gives errors in Processing application

edited December 2013 in JavaScript Mode

I've been using processing so far by editing text .pde files then loading them up in a browser as processing canvases. I decided I wanted to directly use the Processing application to do my code creation. But Processing applicaiton, set to javascript mode, doesn't like my pde code even though it works perfectly as a pde in a web browser.

here's an example snippet where I'm trying to create a javascript object. This works fine when I use it as a .pde file in browser where I have loaded processing.js as well but fails in the processing application when I try to export it. It gives an error message of "Missing a semi colon" and highlights line 1 as the offending line.

function stopwatch() {
    this.running = false;
    this.elapsedTime =  0;
    this.ii = 0;

    };

stopwatch.prototype.reset = function {
     this.running = false;
     this.elapsedTime =  0;   
    };

here's an alternative form I've tried: this give messages like "Unexpected token: = " and highlights the "stopwatch.prototype.reset =" line.

void stopwatch() {
    this.running = false;
    this.elapsedTime =  0;
    this.ii = 0;

    };

  stopwatch.prototype.reset = function() {
     this.running = false;
     this.elapsedTime =  0;   
    };

First I'm baffled what I'm doing wrong.

Second, Why is it that the code I wrote works fine directly in the browser but fails in processing application.

I am using processing with the javacript mode.

---- added ----

Here's a third form that also fails:

var stopwatch = function() {
        this.running = false;
        this.elapsedTime =  0;
        this.ii = 0;

        };

Answers

  • In Java, functions can't be stored in variables! At least, not until Java 8! >-)

  • @GoToLoop

    Thanks for the reply. This is javascript not Java.

    However, I'm new to Processing so maybe for reasons I don't understand your answer still makes sense when you consider that I'm using javascript???

    If so, 1) why is it that I can do this in a browser loaded .pde but not in the application. 2) your answer might be relevant to the second example above, but the first example is giving an error on the first function.

  • You prefix your subjects with jsMode, take an extra step and put them in the JavaScript Mode category!

    I moved them...

  • edited December 2013

    It's a simple matter of language support by the PDE.

    Processing is based on Java and the Processing language uses elements of Java. The PDE compiles it in Java mode to an applet or application, and in JavaScript mode it exports the Processing code as .pde, which is finally translated by the ProcessingJS library to JavaScript. If there is already JavaScript code in your .pde file (as in your example), it is just inserted into the JavaScript code that was translated from Processing.

    The reverse translation from JavaScript back to Processing and Java is not possible, which is the reason why you obtain errors in the PDE.

    As a side note: Currently there is the P5JS project in development, which, as far as I have understood, has as goal to implement Processing functionality as native JavaScript code as opposed to ProcessingJS, which emulates the Processing code using JavaScript.

  • edited December 2013

    You can also add CoffeeScript mode.
    It's a syntactic sugar language which is directly compiled into JavaScript. ;;)

Sign In or Register to comment.