Uncaught Syntaxerror: Unexpected token this

Asked

Viewed 3,099 times

6

When this code is run all together gives this error, but if it is formatted with line breaks it runs normally, how to fix?

Error:

Uncaught Syntaxerror: Unexpected token this

Code in question:

var Recorder = function(source){ this.context = source.context; if(!this.context.createScriptProcessor){ this.node = this.context.createJavaScriptNode(4096, 2, 2); } else { this.node = this.context.createScriptProcessor(4096, 2, 2); } var AW = new Worker('./scr/js/crypto_worker.js'); this.node.onaudioprocess = function(e){ if (!Config.audioRecording) { return; } AW.postMessage({data:{ taskID: 'record', buffer: [ e.inputBuffer.getChannelData(0), e.inputBuffer.getChannelData(1) ] }}); } this.record = function(){ Config.audioRecording = true; Config.analyserCallbackSTA(); } this.stop = function(){ Config.audioRecording = false; } this.clear = function(){ AW.postMessage({data:{ taskID: 'clear' }}); } this.getBuffers = function(cb) { Config.audioCallback = cb; AW.postMessage({data:{ taskID: 'getBuffers' }}); } this.exportWAV = function(callback, type){ Config.audioCallback = callback; type = 'audio/wav'; if (!Config.audioCallback) { Config.audioRecorder.stop(); console.error(dT(), 'Failed in generation of audio/wav'); safeConfirm({ type: 'ERROR_SYS', noBar: true, message: User.LANGUAGE.ERROR_AUDIO_SYS }); return; } AW.postMessage({data:{ taskID: 'exportWAV', type: type }}); } AW.onmessage = function(e){ if(Config.audioCallback) { Config.audioCallback(e.data); } } source.connect(this.node); this.node.connect(this.context.destination); };

1 answer

10


In this code there are several function expressions within a constructor function (which has also been declared as expression). The structure of this code is more or less like this:

var Construtor = function() {
    this.a = function() {

    }
    this.b = function() {

    }
};

When you minify the code, you get an excerpt like this:

... = function(){}this ...

And that’s where the bug is. Function expressions without ; at the end (after the }) have high chances of giving problem in minified code (see this question). This only occurs with function expressions, not with any block { }. For example, this is valid:

if(1){}this

But this is a syntax error:

var f=function(){}this

Moral of the story: place semicolon at the end of function expressions:

var Construtor = function() {
    this.a = function() {

    };
//   ^   
    this.b = function() {

    };
//   ^
};
  • Thank you very much! Something else has entered my knowledge

  • 1

    Good, I’m happy when my answers contribute to people’s knowledge!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.