开发者

What does <| in this code mean?

开发者 https://www.devze.com 2023-03-30 21:01 出处:网络
开发者_运维问答function foo() {} var bar = foo <| function() {}; This is the first time I\'ve seen something like this. What does <| 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


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.


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


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

For A complete list of javascript operators go here

0

精彩评论

暂无评论...
验证码 换一张
取 消