In pursuit of better JavaScript skills, I’ve encountered a Safari problem that may be a limitation in the browser, or it may be my coding. I have a temporary test file demonstrating what’s happening. In Firefox, everything works as I’d hoped it would, but in Safari, all I get is an error on the JavaScript console stating:
SyntaxError – Parse error
Here’s the function in which the error occurs, with the offending line emphasized:
function Test(a,b) {
this.a = a;
this.b = b;
this.c = new (function Inner() {
this.x = 'woo';
});
}
As I say, here’s the temporary test file in case you want to see the entire script.
My long experience with CSS and browser handling of it teaches me that just because something works in one browser, that doesn’t mean it’s supposed to work at all. Therefore, it could be that Firefox is letting me be sloppy, and Safari is telling me I’ve messed up; or it could be that Firefox is right, and Safari has a problem.
So which is it? If it’s the former, how can I do the same thing I’m trying to do, except correctly? If it’s the latter, are there any simple workarounds to get Safari to behave?
Thanks for any help.
Update: Lachlan Hunt rides in to the rescue by pointing out what probably should have been painfully obvious, but like I say, I’m new to this. The solution here:
function Test(a,b) {
this.a = a;
this.b = b;
this.c = new function() {
this.x = 'woo';
};
}
I’m still not 100% certain if this was a case of sloppy authoring or a bad browser, but I’m not so concerned with that right now. I’ll leave up the post in case anyone else encounters a similar problem.
Update redux: Adrian (and, though I didn’t understand what he was saying at the time, Tim) points out a simpler way to do the same thing.