Uncaught SyntaxError: Unexpected token this

5

When this code is run together it 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); };
    
asked by anonymous 13.07.2015 / 23:29

1 answer

9

In this code you have several function expressions within a constructor function (which was also declared as an expression). The structure of this code is something like this:

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

    }
    this.b = function() {

    }
};

When you minify the code, you get a snippet like this:

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

And that's where the error lies. Function expressions without ; at the end (after } ) have a high chance of having problem in mined code (see this question ). This only occurs with function expressions, not with any { } block. For example, this holds true:

if(1){}this

But this is a syntax error:

var f=function(){}this

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

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

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

    };
//   ^
};
    
14.07.2015 / 00:27