Skip to content

Commit

Permalink
Add Properties names section (#75)
Browse files Browse the repository at this point in the history
* Add properties names section

* Add time-span resource

* Fix watch files
  • Loading branch information
Kikobeats authored May 4, 2018
1 parent d56ec8b commit ad9d25c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* V8
* [Hidden Class](v8-tips/hidden-class.md)
* [Sparse Arrays](v8-tips/sparse-arrays.md)
* [Properties Names](v8-tips/properties-names.md)
* [Float Number](v8-tips/float-number.md)
* [Inline initialization](v8-tips/inline-initialization.md)
* [Monomorphic](v8-tips/monomorphic.md)
Expand Down
1 change: 1 addition & 0 deletions docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [tickedoff](https://github.com/jamiebuilds/tickedoff) – Use the faster timer function available for deferring something by a "tick".
* [sliced](https://www.npmjs.com/package/sliced) – A faster Node.js alternative to `Array.prototype.slice.call(arguments)`.
* [time-span](https://github.com/sindresorhus/time-span) – Simplified high resolution timing.

## Cache

Expand Down
61 changes: 61 additions & 0 deletions docs/v8-tips/properties-names.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Properties names

When you use a unquoted property names as `Object` keys, the [specification](http://es5.github.io/x8.html#x8.10) defines that it will be implicitly serialized as `String`:

```js
var object = {
// `abc` is a valid identifier; no quotes are needed
abc: 1,
// `123` is a numeric literal; no quotes are needed
123: 2,
// `π` is a valid identifier; no quotes are needed
π: Math.PI,
// `var` is a valid identifier name (although it’s a reserved word); no quotes are needed
var: 4,
// `foo bar` is not a valid identifier name; quotes are required
'foo bar': 5,
// `foo-bar` is not a valid identifier name; quotes are required
'foo-bar': 6,
// the empty string is not a valid identifier name; quotes are required
'': 7
}
```

When you use of numeric literals, the implicit coerced could be a bit confusing.

For example, if you were to use the number `.12e3` as an (unquoted) property name, it would be coerced into a string first, and the actual object key would become `'120'`.

```js
var object = {
0.12e3: 'wut'
}
object[0.12e3] // 'wut'
object['.12e3'] // undefined
object['120'] // 'wut'
```

!> A [numeric literal](http://es5.github.io/x7.html#x7.8.3) can be either a decimal literal (e.g. `0`, `123`, `123.`, `.123`, `1.23`, `1e23`, `1E-23`, `1e+23`, `12`, but not `01`, `+123` or `-123`) or a hex integer literal (0[xX][0-9a-fA-F]+ in regex, e.g. `0xFFFF`, `0X123`, `0xaBcD`).

In this case, because the `String` representation of the `Number` is different, this is the correct way to acess using bracket notation:

```js
var object = {
12e34: 42
}

object['1.2e+35'] = 42
```

## Common Cases

!> You can easily check the string value of any number with `String(number)` or [Unquoted JavaScript property name validator](https://mothereff.in/js-properties#12e34).

This is a list of common object keys used as `String`:

- `1` will be `'1'`.
- `undefined` will be `'undefined'`.
- `null` will be `'null'`.
- `''` will be `''`.
- `true`/`false` as `'true'`/`'false'`.

Also, **reserverd words** cannot be used as object keys.
7 changes: 1 addition & 6 deletions docs/v8-tips/sparse-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ If you want V8 to represent your `Array` in the `Fast Elements` form, you need t
- Don't delete elements, specially in numeric `Array`s.
- Don't load uninitialized or deleted elements.

Another consideration: any name used as property name that is not a string is stringified via `.toString()`, so:

- `1` will be `'1'`.
- `undefined` will be `'undefined'`.
- `null` will be `'null'`.
- `''` will be `''`.
Another consideration: any name used as property name that is not a `String` will be serialized, read more at [Properties Names](v8-tips/properties-names).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dev": "concurrently \"gulp\" \"npm run server:dev\"",
"lint": "standard-markdown docs/**/*.md !docs/v8-tips/freeing-memory.md !docs/workflow/scope.md",
"pretest": "npm run lint",
"server:dev": "cd docs && browser-sync start --server --files \"index.html, **/*.md main.min.js, style.min.css\"",
"server:dev": "cd docs && browser-sync start --server --files \"index.html, **/*.md, main.min.js, style.min.css\"",
"test": "echo 'no tests yet' && exit 0"
},
"private": true,
Expand Down

0 comments on commit ad9d25c

Please sign in to comment.