سؤال

I am making a JavaScript Game Engine called Rage Engine. After about 1.5 hours of working on Version 1.0 Build 1, I had enough features and decided to do a test. Immediately I ran into a problem. The Console was saying Unexpected token } on line 135. I went to line 135 and found where the Console said there was a unexpected } and removed it. The Console next told me that there was an unexpected else on line 135. I use Sublime Text 2 as my IDLE and I made sure of that there were no brackets that were unintended.

Update:

I have identified, thanks in part to +Jason, what I think to be the problem The Method RageEngine.data.file.requestQuota is too large. I have run into this problem before and I think the javascript compiler cannot handle functions larger than a size without breaking down. I don't know how I could fix that.


Method Error Appeared In:

RageEngine.data.file.requestQuota = function(type,size,success) {
    if (RageEngine.data.file.check().FileSystem) {
        if(type == 'temp') { 
        window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
        window.requestFileSystem(window.TEMPORARY,size,success,function(e) {
            var msg = '', c = 0;

            switch (e.code) {
                case FileError.QUOTA_EXCEEDED_ERR:
                    msg = 'QUOTA_EXCEEDED_ERR';
                    c = 1
                    break;
                case FileError.NOT_FOUND_ERR:
                    msg = 'NOT_FOUND_ERR';
                    c = 2
                    break;
                case FileError.SECURITY_ERR:
                    msg = 'SECURITY_ERR';
                    c = 3
                    break;
                case FileError.INVALID_MODIFICATION_ERR:
                    msg = 'INVALID_MODIFICATION_ERR';
                    c = 4
                    break;
                case FileError.INVALID_STATE_ERR:
                    msg = 'INVALID_STATE_ERR';
                    c = 5
                    break;
                default:
                msg = 'Unknown Error';
                break;
        };
        throw new Error('TEMP QUOTA Error: ' +msg+'. Error #3211.'+c+' '+msg)
        }
    } else if (type == 'perm') {
        window.webkitStorageInfo.requestQuota(PERSISTENT, size, function(grantedBytes) {
            window.requestFileSystem(PERSISTENT, grantedBytes, success, function(e) {
            var msg = '', c = 0;

            switch (e.code) {
                case FileError.QUOTA_EXCEEDED_ERR:
                    msg = 'QUOTA_EXCEEDED_ERR';
                    c = 1
                    break;
                case FileError.NOT_FOUND_ERR:
                    msg = 'NOT_FOUND_ERR';
                    c = 2
                    break;
                case FileError.SECURITY_ERR:
                    msg = 'SECURITY_ERR';
                    c = 3
                    break;
                case FileError.INVALID_MODIFICATION_ERR:
                    msg = 'INVALID_MODIFICATION_ERR';
                    c = 4
                    break;
                case FileError.INVALID_STATE_ERR:
                    msg = 'INVALID_STATE_ERR';
                    c = 5
                    break;
                default:
                msg = 'Unknown Error';
                break;
        };
        throw new Error('PERM QUOTA Error: ' +msg+'. Error #3212.'+c+' '+msg));
            }, function(e) {
            throw new Error('PERM QUOTA Error: '+e+'. Errror #3213 PERM QUOTA Error (in RageEngine.data.file.requestQuota)')
        });
    } else {
        throw new TypeError("Invalid type "+type+". Error #3214 INVALID_TYPE (in RageEngine.data.file.requestQuota)")
    }
    } else {
        throw new ReferenceError("Invalid User Support for FileSystem. Error #011 NO_SUPPORT (in RageEngine.data.file.requestQuota)")
    }

Full Code

var RageEngine = {} // Initalize main object
// Initalize Sub Objects
RageEngine.hardware = {};
RageEngine.canvas = {};
RageEngine.data = {};


// Go Through each one and add methods
//** Sound **\\
RageEngine.hardware.sound.canPlayType = function(file) {

        var audioElement = document.createElement( 'audio' );
        return !!( audioElement.canPlayType && 
                   audioElement.canPlayType( 'audio/' + file.split( '.' ).pop().toLowerCase() + ';' ).replace( /no/, '' ) );

}
RageEngine.hardware.sound.preload = function(url) {
if(RageEngine.sound.canPlayType(url.split(".")[url.split(".").length - 1])) { // Test if the computer can play that type of audio
audio = new Audio(url)
return audio
} else {
    throw new TypeError("Cannot load "+url+". Error: #111 USER_CANNOT_PLAY (in RageEngine.hardware.sound.preload)")
}
return audio
}
//** Video and Audio**\\
RageEngine.hardware.userMedia = {}
RageEngine.hardware.userMedia.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia || navigator.msGetUserMedia
RageEngine.hardware.userMedia.check = function() {
    return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia || navigator.msGetUserMedia);
}
RageEngine.hardware.userMedia.get = function(video,audio,stream_options) {
    if (!RageEngine.hardware.userMedia.check()) {
        throw new Error("Invalid Support for getUserMedia. Error #011 NO_SUPPORT (in RageEngine.hardware.userMedia.get)")
    } else {
        if(video || audio != (true||false)) throw new TypeError('Invalid Video or Audio type. Expecting Boolean (RageEngine.hardware.userMedia.get)')
        if (stream_options.type == "auto") {
            RageEngine.hardware.userMedia.getUserMedia({video:video,audio:audio},function(mediastream) {
                var video = document.createElement("VIDEO");
                video.src = window.URL.createObjectURL(localMediaStream);
                stream_options.stream(video)
            });
        } else if (stream_options.type == "man") {
            RageEngine.hardware.userMedia.getUserMedia({video:video,audio:audio},stream_options.stream);
        }

    }
}
//** Canvas **\\
// Canvas API added to be added in v1.1
RageEngine.canvas.canvas = function() {
    console.warn('Canvas API is not implemented yet (in RageEngine.canvas.canvas)')
}
//** Data **\\

RageEngine.data.string = {};
RageEngine.data.string.store = function(type,key,value) {
    switch(type.toLowerCase()) {
        case "temp":
            sessionStorage[key] = value;
            break;
        case "perm":
            localStorage[key] = value;
            break;
        default:
            throw new TypeError("Invalid type "+type+". Error: #2111 INVAILD_STORE_TYPE (in RageEngine.data.string.store)")
    }
}
RageEngine.data.string.recall = function(type,key) {
    switch(type.toLowerCase()) {
        case "temp":
            return sessionStorage[key]
            break;
        case "perm":
            return localStorage[key]
            break;
        default:
            throw new TypeError("Invalid type "+type+". Error: #2112 INVAILD_RECALL_TYPE (in RageEngine.data.string.store)")
    }
}
RageEngine.data.string.check = function() {
    var support = {
        'temp': sessionStorage==undefined ? false : true, 
        'perm': localStorage==undefined ? false : true
    };
    return support
}
RageEngine.data.file = {};
RageEngine.data.file.check = function() {
    var support = {
        'File':window.File==undefined ? false : true,
        'FileReader':window.FileReader==undefined ? false : true,
        'FileList':window.FileList==undefined ? false : true,
        'Blob':window.Blob==undefined ? false : true,
        'FileSystem':window.FileSystem==undefined ? false : true 
    }
    return support
}
RageEngine.data.file.requestQuota = function(type,size,success) {
    if (RageEngine.data.file.check().FileSystem) {
        if(type == 'temp') { 
        window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
        window.requestFileSystem(window.TEMPORARY,size,success,function(e) {
            var msg = '', c = 0;

            switch (e.code) {
                case FileError.QUOTA_EXCEEDED_ERR:
                    msg = 'QUOTA_EXCEEDED_ERR';
                    c = 1
                    break;
                case FileError.NOT_FOUND_ERR:
                    msg = 'NOT_FOUND_ERR';
                    c = 2
                    break;
                case FileError.SECURITY_ERR:
                    msg = 'SECURITY_ERR';
                    c = 3
                    break;
                case FileError.INVALID_MODIFICATION_ERR:
                    msg = 'INVALID_MODIFICATION_ERR';
                    c = 4
                    break;
                case FileError.INVALID_STATE_ERR:
                    msg = 'INVALID_STATE_ERR';
                    c = 5
                    break;
                default:
                msg = 'Unknown Error';
                break;
        };
        throw new Error('TEMP QUOTA Error: ' +msg+'. Error #3211.'+c+' '+msg)
        }
        } else if (type == 'perm') {
        window.webkitStorageInfo.requestQuota(PERSISTENT, size, function(grantedBytes) {
            window.requestFileSystem(PERSISTENT, grantedBytes, success, function(e) {
            var msg = '', c = 0;

            switch (e.code) {
                case FileError.QUOTA_EXCEEDED_ERR:
                    msg = 'QUOTA_EXCEEDED_ERR';
                    c = 1
                    break;
                case FileError.NOT_FOUND_ERR:
                    msg = 'NOT_FOUND_ERR';
                    c = 2
                    break;
                case FileError.SECURITY_ERR:
                    msg = 'SECURITY_ERR';
                    c = 3
                    break;
                case FileError.INVALID_MODIFICATION_ERR:
                    msg = 'INVALID_MODIFICATION_ERR';
                    c = 4
                    break;
                case FileError.INVALID_STATE_ERR:
                    msg = 'INVALID_STATE_ERR';
                    c = 5
                    break;
                default:
                msg = 'Unknown Error';
                break;
        };
        throw new Error('PERM QUOTA Error: ' +msg+'. Error #3212.'+c+' '+msg));
            }, function(e) {
            throw new Error('PERM QUOTA Error: '+e+'. Errror #3213 PERM QUOTA Error (in RageEngine.data.file.requestQuota)')
        });
    } else {
        throw new TypeError("Invalid type "+type+". Error #3214 INVALID_TYPE (in RageEngine.data.file.requestQuota)")
    }
    } else {
        throw new ReferenceError("Invalid User Support for FileSystem. Error #011 NO_SUPPORT (in RageEngine.data.file.requestQuota)")
    }

Thank you,
--Vulpus
I know the code is sloppy, but this is Version 1 Build 1.
BTW: The code is live at http://www.vulpusinc.co.nf/RageEngine/J/RageEngine.js or http://www.vulpusinc.co.nf/RageEngine/J/RageEngine.min.js (4Kb of 68% smaller!). Feel Free to use it

هل كانت مفيدة؟

المحلول

Didn't analyze the whole file, but you have an extra end parenthesis which could cause errors on following lines;

throw new Error('PERM QUOTA Error: ' +msg+'. Error #3212.'+c+' '+msg));
                                                        // Extra ')' ^

نصائح أخرى

It looks like you have a whole bunch of mismatched braces. For example:

window.requestFileSystem(window.TEMPORARY,size,success,function(e) {
    //Stuff
}
} else if (type == 'perm')

You're missing some parentheses to properly complete that function call.

The easiest way to fix a problem like this is to have Sublime fix your indenting for you; if some blocks get misaligned, that's a good clue that you have some parenthesis issues. This article gives you a way to do that

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top