21

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?

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
Bergi
  • 630,263
  • 148
  • 957
  • 1,375

1 Answers1

7

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 )

Swivel
  • 3,020
  • 26
  • 36
  • 1
    Thanks, interesting that Chrome wouldn't support such functions at all. Yet it really does not answer the question "Are there such functions?" (as you can't code a `myFunc`, it must be a host object) – Bergi Oct 30 '12 at 17:27
  • Indeed. I don't have the rep to comment, so I figured I'd try and take a whack at the answer. The answer to "Are there such functions?" is Yes. The non-support by Chrome is actually a bug rather than a feature left out or purposeful unsupporting of a particular implementation. And yeah, the example was a bit sketchy. Just threw it together to try and explain as best as possible. :) – Swivel Oct 30 '12 at 19:19
  • Seems like ES6 made this actually an early error and therefore v8 follows the spec now ... (see the linked bug) – Jonas Wilms Aug 02 '18 at 19:39
  • @Bergi If this answer is appropriate for your question, do you mind accepting it? – Swivel Aug 07 '18 at 20:04
  • @Swivel No, I don't think this answers my question for "*host functions which result in Reference values*". – Bergi Aug 08 '18 at 14:33
  • @Bergi Apologies. Re-read the question. You're absolutely correct. – Swivel Aug 13 '18 at 21:28