简体   繁体   English

什么<|在这段代码意味着什么

[英]What does <| in this code mean?

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 资料来源: https//github.com/allenwb/ESnext-experiments/blob/master/ST80collections-exp1.js

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 ): 现在您已经发布了指向源的链接,您可以在文件顶部的注释中看到它的确切内容( 第36行 ):

the <| <| operator -- defines the [[Prototype]] of a literal... operator - 定义文字的[[Prototype]] ...

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. 与函数表达式一起使用时,将作为函数“prototype”属性的值创建的对象的[[Prototype]]设置为LHS对象的“prototype”属性的值。 This is in addition to setting the [[Prototype]] of the function object itself. 这是设置函数对象本身的[[Prototype]]的补充。 In other words, it builds sets the [[Prototype]] of both the function and of function.prototype to potentially different values. 换句话说,它构建将函数和function.prototype的[[Prototype]]设置为可能不同的值。

Update: I've just remembered this question as I came across the full ECMAScript Harmony proposal for this "literal [[Prototype]] operator" . 更新:我刚刚记得这个问题,因为我遇到了这个“文字[[Prototype]]运算符”完整ECMAScript Harmony提案 There is a lot more information in there than in the quote above, so it's worth a read. 这里有更多的信息,而不是上面的引文,所以值得一读。

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. 如果定义了foo,则将foo分配给bar,否则分配空函数。

About the link you posted later, it is still not valid Javascript. 关于您之后发布的链接,它仍然无效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. 该项目包含各种语言扩展的示例文件,这些文件正在考虑包含在下一版的ECMA语言规范中。 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

That throws a syntax error for me ("unexpected token" on the "|") 这会为我抛出语法错误(“|”上的“意外令牌”)

For A complete list of javascript operators go here 有关javascript运算符的完整列表,请转到此处

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

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