admin管理员组

文章数量:1389757

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?

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?

Share Improve this question edited Oct 31, 2020 at 1:40 shreyasm-dev 2,8845 gold badges23 silver badges39 bronze badges asked Oct 29, 2012 at 15:14 BergiBergi 666k161 gold badges1k silver badges1.5k bronze badges 3
  • 3 Related? stackoverflow./questions/3709866/… – bfavaretto Commented Oct 29, 2012 at 17:14
  • Yes, I think so. That question proves the syntax I used above would be valid :-) – Bergi Commented Oct 29, 2012 at 17:44
  • I just googled "returns a reference" javascript and looked through the first through pages, but there was nothing. – Nathan Commented Oct 29, 2012 at 19:07
Add a ment  | 

1 Answer 1

Reset to default 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./p/v8/issues/detail?id=838 )

本文标签: javascriptReal world examples of Ecmascript functions returning a ReferenceStack Overflow