Ecmascript 函数返回引用的真实世界示例? [英] Real world examples of Ecmascript functions returning a Reference?

查看:24
本文介绍了Ecmascript 函数返回引用的真实世界示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ECMAScript 规范,第 8.7 节 参考规范类型状态:

Reference 类型用于解释诸如deletetypeof 和赋值运算符等运算符的行为.[…] 引用是解析的名称绑定.

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.

允许函数调用返回引用.承认这种可能性纯粹是为了宿主对象.本规范定义的内置 ECMAScript 函数没有返回引用,也没有规定用户定义的函数返回引用.

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.

最后两句话让我印象深刻.有了这个,你可以做像 coolHostFn() = value(valid syntax,顺便说一句)之类的事情.所以我的问题是:

Those last two sentences impressed me. With this, you could do things like coolHostFn() = value (valid syntax, btw). So my question is:

是否有任何 ECMAScript 实现定义了导致 Reference 值的宿主函数对象?

Are there any ECMAScript implementations that define host function objects which result in Reference values?

推荐答案

Google Chrome 的引擎非常适合这种方式.但是,您会在控制台中注意到,在执行以下操作时,您将收到 ReferenceError: Invalid left-hand side in assignment:

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");

然而,这是一个早期错误,因为 v8 的 ECMAScript 实现,如果它在假设引用错误之前正确执行 myFunc,这应该可以工作.

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 的当前实现中?是和否.它是默认实现的(由于语言的结构方式),但是该功能因不同的问题而停止.coolHostFn() = value 不应返回错误,并且确实应该能够正确执行.然而,3=4 肯定会返回左侧赋值错误.

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.

(这是问题/票,以防有人想插话... http://code.google.com/p/v8/issues/detail?id=838 )

(Here's the Issue/Ticket in case anyone wants to chime in... http://code.google.com/p/v8/issues/detail?id=838 )

这篇关于Ecmascript 函数返回引用的真实世界示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆