Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real world examples of Ecmascript functions returning a Reference?

Tags:

ECMAScript specification, section 8.7 The Reference Specification Type states:

The Reference type is used to explain the behaviour of such operators as delete, 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?

like image 512
Bergi Avatar asked Oct 29 '12 15:10

Bergi


People also ask

How do you return a value from a JavaScript function?

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.

What does () => mean in JavaScript?

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.

What are JavaScript references?

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.

What are the types of functions in JavaScript?

There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.


1 Answers

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 )

like image 75
Swivel Avatar answered Nov 09 '22 02:11

Swivel