<| 是什么意思?这段代码的意思是?

发布于 2024-12-01 12:23:18 字数 333 浏览 1 评论 0 原文

function foo() {}
var bar = foo <| function() {};

这是我第一次看到这样的事情。 <| 是什么意思?

来源:https://github.com/allenwb/ESnext-experiments /blob/master/ST80collections-exp1.js

function foo() {}
var bar = foo <| function() {};

This is the first time I've seen something like this. What does <| mean?

Source: https://github.com/allenwb/ESnext-experiments/blob/master/ST80collections-exp1.js

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅气称霸 2024-12-08 12:23:18

现在您已经发布了源链接,您可以在文件顶部的注释中确切地看到它的作用(第 36 行):

<|运算符 -- 定义文字的 [[Prototype]]...

对于这些例子<|与函数表达式一起使用设置
作为函数的值创建的对象的 [[Prototype]]
将“prototype”属性设置为“prototype”属性的值
LHS 对象。这是除了设置 [[Prototype]] 之外的
函数对象本身。换句话说,它构建了
函数和 function.prototype 的 [[Prototype]]
可能有不同的值。

更新:当我遇到完整的 ECMAScript Harmony 时,我才想起这个问题针对此“文字 [[Prototype]] 运算符”的提案。其中的信息比上面引用的信息多得多,因此值得一读。

Now that you have posted the link to the source, you can see in the comments at the top of the file exactly what it does (line 36):

the <| operator -- defines the [[Prototype]] of a literal...

For these examples <| used with a function expression sets the
[[Prototype]] of the object created as the value of the function's
"prototype" property to the value of the "prototype" property of the
the LHS object. This is in addition to setting the [[Prototype]] of
the function object itself. In other words, it builds sets the
[[Prototype]] of both the function and of function.prototype to
potentially different values.

Update: I've just remembered this question as I came across the full ECMAScript Harmony proposal for this "literal [[Prototype]] operator". There is a lot more information in there than in the quote above, so it's worth a read.

很酷不放纵 2024-12-08 12:23:18

看起来应该是,

function foo() {}
var bar = foo || function() {};

如果定义了 foo,则将 foo 分配给 bar,否则将一个空函数分配给 bar。

关于您稍后发布的链接,它仍然是无效的Javascript。该项目的自述文件解释了该文件的用途。

该项目包含各种语言扩展的示例文件
正在考虑将其纳入下一版本的
ECMA 语言规范。示例的目的是测试
建议功能的实用性、可写性和可读性。有
不保证其中任何一个实际上会被纳入
语言。

建议功能的描述将您粘贴到问题中的代码行括起来。

the <| operator -- defines the [[Prototype]] of a literal

/* Quote that James posted */

function foo() {};
const bar = foo <| function() {};

Object.getPrototypeOf(bar)===foo; //true
Object.getPrototypeOf(bar.prototype)===foo.prototype;  //true

It looks like it should be

function foo() {}
var bar = foo || function() {};

Which will assign foo to bar, if foo is defined and assign an empty function to bar otherwise.

About the link you posted later, it is still not valid Javascript. The project's README explains the purpose of the file.

This project contains example files of the various language extensions
that are being considered for inclusion in the next editions of the
ECMA Language Specification. The purpose of examples is to test the
utility, writability, and readability of proposed features. There is
no guarentee that any of these will actually be incorporated into the
language.

A description of the proposed functionality brackets the lines of code you pasted into your question.

the <| operator -- defines the [[Prototype]] of a literal

/* Quote that James posted */

function foo() {};
const bar = foo <| function() {};

Object.getPrototypeOf(bar)===foo; //true
Object.getPrototypeOf(bar.prototype)===foo.prototype;  //true
烟燃烟灭 2024-12-08 12:23:18

这给我带来了一个语法错误(“|”上的“意外标记”)

要获取 JavaScript 运算符的完整列表,请访问此处

That throws a syntax error for me ("unexpected token" on the "|")

For A complete list of javascript operators go here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文