简体   繁体   English

返回引用的 Ecmascript 函数的真实示例?

[英]Real world examples of Ecmascript functions returning a Reference?

ECMAScript specification, section 8.7 The Reference Specification Type states: ECMAScript 规范,第 8.7 节参考规范类型指出:

The Reference type is used to explain the behaviour of such operators as delete , typeof , and the assignment operators. Reference类型用于解释deletetypeof和赋值运算符等运算符的行为。 […] 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.本规范定义的内置 ECMAScript 函数不返回引用,也没有规定用户定义的函数返回引用。

Those last two sentences impressed me.最后两句话让我印象深刻。 With this, you could do things like coolHostFn() = value ( valid syntax , btw).有了它,您可以执行诸如coolHostFn() = value有效语法,顺便说一句)之类的事情。 So my question is:所以我的问题是:

Are there any ECMAScript implementations that define host function objects which result in Reference values?是否有任何 ECMAScript 实现定义了导致Reference值的宿主函数对象?

Google Chrome's engine works very much in this way. Google Chrome 的引擎以这种方式工作。 However, you'll notice in the console you'll get an ReferenceError: Invalid left-hand side in assignment when executing the following:但是,您会注意到在执行以下操作时,您会在控制台中收到ReferenceError: Invalid left-hand side in assignment

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.然而,这是一个早期错误,并且由于 v8 的 ECMAScript 实现,如果它在假设引用错误之前正确执行myFunc ,这应该可以工作。

So, in v8's current implementation?那么,在 v8 当前的实现中? 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. coolHostFn() = value不应返回错误,并且确实应该能够正确执行。 However 3=4 should most certainly return a left-hand side assignment error.但是3=4肯定会返回左侧分配错误。

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 ) (这是 Issue/Ticket,以防有人想插话...... http://code.google.com/p/v8/issues/detail?id=838

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM