什么

时间:2022-09-06 14:30:22
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

3 个解决方案

#1


34  

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. 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.

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

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.

更新:我刚刚记得这个问题,因为我遇到了这个“文字[[Prototype]]运算符”的完整ECMAScript Harmony提案。这里有更多的信息,而不是上面的引文,所以值得一读。

#2


7  

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. The project's README explains the purpose of the file.

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

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.

该项目包含各种语言扩展的示例文件,这些文件正在考虑包含在下一版的ECMA语言规范中。示例的目的是测试所提议特征的效用,可写性和可读性。没有任何保证,任何这些实际上将被纳入该语言。

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

#3


0  

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

这会为我抛出语法错误(“|”上的“意外令牌”)

For A complete list of javascript operators go here

有关javascript运算符的完整列表,请转到此处

#1


34  

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. 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.

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

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.

更新:我刚刚记得这个问题,因为我遇到了这个“文字[[Prototype]]运算符”的完整ECMAScript Harmony提案。这里有更多的信息,而不是上面的引文,所以值得一读。

#2


7  

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. The project's README explains the purpose of the file.

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

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.

该项目包含各种语言扩展的示例文件,这些文件正在考虑包含在下一版的ECMA语言规范中。示例的目的是测试所提议特征的效用,可写性和可读性。没有任何保证,任何这些实际上将被纳入该语言。

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

#3


0  

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

这会为我抛出语法错误(“|”上的“意外令牌”)

For A complete list of javascript operators go here

有关javascript运算符的完整列表,请转到此处