ECMAScript specification, section 8.7 The Reference Specification Type states:
The
Reference
type is used to explain the behaviour of such operators asdelete
,typeof
, and the assignment operators. […] A Reference is a resolved name binding.Function calls are permitted to return references. This possibility is admitted purely for the sake of host objects. No built-in ECMAScript function defined by this specification returns a reference and there is no provision for a user-defined function to return a reference.
Those last two sentences impressed me. With this, you could do things like coolHostFn() = value
(valid syntax, btw). So my question is:
Are there any ECMAScript implementations that define host function objects which result in Reference
values?
JavaScript passes a value from a function back to the code that called it by using the return statement. The value to be returned is specified in the return. That value can be a constant value, a variable, or a calculation where the result of the calculation is returned.
It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.
In JavaScript, unlike in most other popular programming languages, the references are pointers to values stored in variables and NOT pointers to other variables, or references.
There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.
Google Chrome's engine works very much in this way. However, you'll notice in the console you'll get an ReferenceError: Invalid left-hand side in assignment
when executing the following:
var myObj = new Object();
function myFunc() {
myObj.test = "blah";
return myObj;
}
myFunc() = new String("foobar");
This is an Early Error, however, and because the v8's ECMAScript implementation, this should work if it properly executes myFunc
before assuming the reference error.
So, in v8's current implementation? Yes and No. It is implemented by default (due to how the language is structured), however the capability is halted by a different issue. coolHostFn() = value
should not return an error, and should indeed be able to execute properly. However 3=4
should most certainly return a left-hand side assignment error.
Not exactly an answer to your question, but I hope it helps clarify why it doesn't work.
(Here's the Issue/Ticket in case anyone wants to chime in... http://code.google.com/p/v8/issues/detail?id=838 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With