Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

javascript - Real world examples of Ecmascript functions returning a Reference?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 )


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...