-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code samples #96
Code samples #96
Changes from 8 commits
030db8b
bfe4c8f
11fe2a8
4b5614f
d1563c2
51a0bdd
048e058
d0e74ba
9e03c99
deb8b78
092edbd
711fd83
a2884dc
8fed776
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,13 @@ | |
tests: | ||
- https://github.com/tc39/test262/issues/1163 | ||
specification: https://tc39.github.io/Function-prototype-toString-revision/ | ||
example: > | ||
// String's parse must contains the same | ||
// function body and parameter list as the original. | ||
|
||
O.gOPD({ get a(){} }, "a").get // "function a(){}" | ||
|
||
O.gOPD({ set a(b){} }, "a").set // "function a(b){}" | ||
resources: | ||
- https://github.com/tc39/Function-prototype-toString-revision | ||
|
||
|
@@ -67,6 +74,14 @@ | |
- Daniel Ehrenberg | ||
tests: | ||
- https://github.com/tc39/test262/issues/1056 | ||
example: > | ||
const theBiggestInt = 9007199254740991n; | ||
|
||
const alsoHuge = BigInt(9007199254740991); | ||
// ↪ 9007199254740991n | ||
|
||
const hugeButString = BigInt('9007199254740991'); | ||
// ↪ 9007199254740991n | ||
resources: | ||
- https://github.com/tc39/proposal-bigint | ||
|
||
|
@@ -91,6 +106,32 @@ | |
- Kevin Gibbons | ||
tests: | ||
- https://github.com/tc39/test262/issues/1343 | ||
example: > | ||
class Counter extends HTMLElement { | ||
#xValue = 0; | ||
|
||
get #x() { return #xValue; } | ||
set #x(value) { | ||
this.#xValue = value; | ||
window.requestAnimationFrame(this.#render.bind(this)); | ||
} | ||
|
||
#clicked() { | ||
this.#x++; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
this.onclick = this.#clicked.bind(this); | ||
} | ||
|
||
connectedCallback() { this.#render(); } | ||
|
||
#render() { | ||
this.textContent = this.#x.toString(); | ||
} | ||
} | ||
window.customElements.define('num-counter', Counter); | ||
resources: | ||
- https://github.com/tc39/proposal-private-methods | ||
|
||
|
@@ -102,6 +143,10 @@ | |
champions: | ||
- Brian Terlson | ||
- Michael Ficarra | ||
example: > | ||
[1, [2, [3]]].flat(Infinity); // [1,2,3] | ||
|
||
[2, 3, 4].flatMap((x) => [x, x * 2]); // [[2, 4, 3, 6, 4, 8] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’d probably be helpful to include a flatMap example that illustrates that it has a depth of 1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This output looks good to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The output has a syntax error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was fixed. |
||
resources: | ||
- https://github.com/tc39/proposal-flatMap | ||
|
||
|
@@ -143,6 +188,10 @@ | |
- Sebastian Markbåge | ||
tests: | ||
- https://github.com/tc39/test262/pull/1246 | ||
example: > | ||
" Hey JS!".trimStart(); // "Hey JS!" | ||
|
||
"Hey JS! ".trimEnd(); // "Hey JS!" | ||
resources: | ||
- https://tc39.github.io/proposal-string-left-right-trim/ | ||
|
||
|
@@ -155,6 +204,29 @@ | |
tests: | ||
- https://github.com/tc39/test262/pull/1500 | ||
- https://github.com/tc39/test262/pull/1587 | ||
example: > | ||
var str = 'Hello world!!!'; | ||
|
||
var regexp = /(\w+)\W*/g; | ||
|
||
console.log(str.matchAll(regexp)); | ||
|
||
/* | ||
[ | ||
{ | ||
0: "Hello ", | ||
1: "Hello" | ||
index: 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These match objects now also contain a “groups” property (which is undefined if there’s no named captures) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also I’m not sure how to illustrate better that these are not objects, but arrays with string properties There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. //cc @sebmarkbage :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m the champion of matchAll, and @mathiasbynens was the champion of named capture groups. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I’ve always done it this way, which matches the Node.js REPL:
The only downside is that it’s not valid JavaScript syntax. |
||
input: "Hello world!!!" | ||
}, | ||
{ | ||
0: "world!!!", | ||
1: "world" | ||
index: 6, | ||
input: "Hello world!!!" | ||
} | ||
] | ||
*/ | ||
resources: | ||
- https://tc39.github.io/proposal-string-matchall/ | ||
|
||
|
@@ -166,6 +238,12 @@ | |
- Michael Ficarra | ||
tests: | ||
- https://github.com/tc39/test262/pull/1590 | ||
example: > | ||
const symbol = Symbol('TC39'); | ||
|
||
console.log(symbol.description); // 'TC39' | ||
|
||
console.log(symbol.hasOwnProperty('description')); // false | ||
resources: | ||
- https://tc39.github.io/proposal-Symbol-description/ | ||
|
||
|
@@ -176,6 +254,15 @@ | |
champions: | ||
- Jordan Harband | ||
- Kevin Gibbons | ||
example: > | ||
const entries = new Map([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think it might be a bit confusing to call a Map There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or, should we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That could work too - but it's also nice to show that you can do it with a Map. Maybe both examples? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will just rename it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. |
||
['cat', 'dog'], | ||
['life', 42] | ||
]); | ||
|
||
const obj = Object.fromEntries(entries); | ||
|
||
console.log(obj); // { cat: "dog", life: 42 } | ||
tests: | ||
- https://github.com/tc39/test262/pull/1677 | ||
- https://github.com/tc39/test262/pull/1676 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For BigInt, I'd prefer we contrast the Number behavior with the BigInt behavior, and not include a use of the BigInt constructor on a Number (as that could lead people to use a buggy pattern with rounding).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @sarahgp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I'm not sure what you mean @littledan. I think showing how the constructor works with a
Number
literal is pretty necessary, in general, but if you mean just for the case of this example, we could definitely use something like this instead:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, I am afraid that people could develop false expectations about how the BigInt constructor works, and try to use it with large number literals. I have already gotten a few "bug reports" about this. I am not sure how we could use code samples to avoid the issue; I am not an expert here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I think this with maybe a final line showing it re-rounded? There is nothing bad about showing people how the BigInt constructor is used — not everyone is going to use literals, right?
To some extent, I think we are going to have to accept that people are going to make some mistakes as they get used to new functionality — and then it will become just a known fact about Javascript. To me that's as expected. But then, I also don't get "bug report"s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I just want to emphasize the happy path. Do you see a reason why someone should use the BigInt constructor together with a Number literal? I was imagining that that was subsumed by BigInt literals, but maybe I am missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could image using the
BigInt
constructor with a number literal if you were programmatically piping data into some calculations with knownBigInt
s, where the literal wouldn't really work. E.g.