Friday, May 26, 2006

ECMAScript reference values

Try this in SpiderMonkey or Firefox:
function foo(flag) {
if (flag) {
print('hello') = 10
}
return 42
}

foo(false) // 42
foo(true) // SyntaxError: invalid assignment left-hand side

3 comments:

Anonymous said...

Umm, I don't get it I guess, but why should you be able to assign 10 to the print function?

Dave Herman said...

You shouldn't, of course. What's fascinating, though, is that a SyntaxError is a runtime exception. Notice how in the first call, no error occurs.

Anonymous said...

Spidermonkey has a config option 'JS_HAS_LVALUE_RETURN' that lets native functions return an lvalue. Currenty all versions of javascript that it can build have this flag enabled.

It lets you define functions that return references directly as long as you define them in C.

I presume someone found a use for it at some point, but it does lead to deferring the SyntaxError until runtime.