From 3db79652f43f0d5b4bd3bc0f233ed7670d91cbdf Mon Sep 17 00:00:00 2001 From: Onkar Ruikar <87750369+OnkarRuikar@users.noreply.github.com> Date: Mon, 24 Oct 2022 15:21:08 +0530 Subject: [PATCH] Run Prettier on JS code fences, part 13 (#21778) The PR focuses only on JS code fences. Idea is to gradually prettify all the JS code fences before the full automation. --- .../proxy/proxy/deleteproperty/index.md | 29 +++++++++-------- .../global_objects/proxy/proxy/get/index.md | 22 +++++++------ .../proxy/getownpropertydescriptor/index.md | 22 +++++++------ .../proxy/proxy/getprototypeof/index.md | 16 +++++----- .../global_objects/proxy/proxy/has/index.md | 22 +++++++------ .../global_objects/proxy/proxy/index.md | 6 ++-- .../proxy/proxy/isextensible/index.md | 29 ++++++++++------- .../proxy/proxy/ownkeys/index.md | 26 +++++++++------- .../proxy/proxy/preventextensions/index.md | 31 ++++++++++++------- .../global_objects/proxy/proxy/set/index.md | 23 ++++++++------ .../proxy/proxy/setprototypeof/index.md | 10 +++--- .../global_objects/rangeerror/index.md | 6 ++-- .../rangeerror/rangeerror/index.md | 4 ++- .../global_objects/reflect/apply/index.md | 6 ++-- .../global_objects/reflect/construct/index.md | 15 +++++---- .../reflect/defineproperty/index.md | 2 +- .../reflect/deleteproperty/index.md | 8 ++--- .../reflect/getownpropertydescriptor/index.md | 10 +++--- .../reflect/getprototypeof/index.md | 20 ++++++------ .../reference/global_objects/reflect/index.md | 14 ++++----- .../reflect/isextensible/index.md | 12 +++---- .../reflect/preventextensions/index.md | 8 ++--- .../global_objects/reflect/set/index.md | 28 ++++++++++------- .../global_objects/regexp/@@match/index.md | 14 ++++----- .../global_objects/regexp/@@replace/index.md | 16 +++++----- .../global_objects/string/replaceall/index.md | 15 ++++----- .../global_objects/string/slice/index.md | 26 ++++++++-------- .../symbol/toprimitive/index.md | 16 +++++----- .../global_objects/typedarray/index.md | 18 +++++------ .../global_objects/typedarray/name/index.md | 20 ++++++------ 30 files changed, 270 insertions(+), 224 deletions(-) diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/deleteproperty/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/deleteproperty/index.md index 076cc76062a225c..9f88b7a5885de9f 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/deleteproperty/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/deleteproperty/index.md @@ -68,24 +68,27 @@ If the following invariants are violated, the proxy will throw a The following code traps the {{jsxref("Operators/delete", "delete")}} operator. ```js -const p = new Proxy({}, { - deleteProperty(target, prop) { - if (!(prop in target)) { - console.log(`property not found: ${prop}`); - return false; - } - delete target[prop]; - console.log(`property removed: ${prop}`); - return true; - }, -}); +const p = new Proxy( + {}, + { + deleteProperty(target, prop) { + if (!(prop in target)) { + console.log(`property not found: ${prop}`); + return false; + } + delete target[prop]; + console.log(`property removed: ${prop}`); + return true; + }, + } +); p.a = 10; -console.log('a' in p); // true +console.log("a" in p); // true const result1 = delete p.a; // "property removed: a" console.log(result1); // true -console.log('a' in p); // false +console.log("a" in p); // false const result2 = delete p.a; // "property not found: a" console.log(result2); // false diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/get/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/get/index.md index ec8ba40ba36f529..5d88d8f919ae553 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/get/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/get/index.md @@ -73,22 +73,26 @@ If the following invariants are violated, the proxy will throw a The following code traps getting a property value. ```js -const p = new Proxy({}, { - get(target, property, receiver) { - console.log(`called: ${property}`); - return 10; - }, -}); +const p = new Proxy( + {}, + { + get(target, property, receiver) { + console.log(`called: ${property}`); + return 10; + }, + } +); -console.log(p.a); // "called: a" - // 10 +console.log(p.a); +// "called: a" +// 10 ``` The following code violates an invariant. ```js const obj = {}; -Object.defineProperty(obj, 'a', { +Object.defineProperty(obj, "a", { configurable: false, enumerable: false, value: 10, diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/getownpropertydescriptor/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/getownpropertydescriptor/index.md index 8540f0b9467f70a..a59daaa4a7c9a8a 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/getownpropertydescriptor/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/getownpropertydescriptor/index.md @@ -66,15 +66,19 @@ If the following invariants are violated, the proxy will throw a {{jsxref("TypeE The following code traps {{jsxref("Object.getOwnPropertyDescriptor()")}}. ```js -const p = new Proxy({ a: 20}, { - getOwnPropertyDescriptor(target, prop) { - console.log(`called: ${prop}`); - return { configurable: true, enumerable: true, value: 10 }; - }, -}); +const p = new Proxy( + { a: 20 }, + { + getOwnPropertyDescriptor(target, prop) { + console.log(`called: ${prop}`); + return { configurable: true, enumerable: true, value: 10 }; + }, + } +); -console.log(Object.getOwnPropertyDescriptor(p, 'a').value); // "called: a" - // 10 +console.log(Object.getOwnPropertyDescriptor(p, "a").value); +// "called: a" +// 10 ``` The following code violates an invariant. @@ -88,7 +92,7 @@ const p = new Proxy(obj, { }, }); -Object.getOwnPropertyDescriptor(p, 'a'); // TypeError is thrown +Object.getOwnPropertyDescriptor(p, "a"); // TypeError is thrown ``` ## Specifications diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/getprototypeof/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/getprototypeof/index.md index 37c4cf0bdee3234..eb7c0593c8de73d 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/getprototypeof/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/getprototypeof/index.md @@ -69,14 +69,14 @@ const obj = {}; const proto = {}; const handler = { getPrototypeOf(target) { - console.log(target === obj); // true + console.log(target === obj); // true console.log(this === handler); // true return proto; }, }; const p = new Proxy(obj, handler); -console.log(Object.getPrototypeOf(p) === proto); // true +console.log(Object.getPrototypeOf(p) === proto); // true ``` ### Five ways to trigger the getPrototypeOf trap @@ -89,11 +89,11 @@ const p = new Proxy(obj, { }, }); console.log( - Object.getPrototypeOf(p) === Array.prototype, // true + Object.getPrototypeOf(p) === Array.prototype, // true Reflect.getPrototypeOf(p) === Array.prototype, // true - p.__proto__ === Array.prototype, // true - Array.prototype.isPrototypeOf(p), // true - p instanceof Array, // true + p.__proto__ === Array.prototype, // true + Array.prototype.isPrototypeOf(p), // true + p instanceof Array // true ); ``` @@ -103,8 +103,8 @@ console.log( const obj = {}; const p = new Proxy(obj, { getPrototypeOf(target) { - return 'foo'; - } + return "foo"; + }, }); Object.getPrototypeOf(p); // TypeError: "foo" is not an object or null diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/has/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/has/index.md index ede1b17d58e0042..5fad4e96a5cf682 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/has/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/has/index.md @@ -70,15 +70,19 @@ If the following invariants are violated, the proxy will throw a The following code traps the {{jsxref("Operators/in", "in")}} operator. ```js -const p = new Proxy({}, { - has(target, prop) { - console.log(`called: ${prop}`); - return true; - }, -}); +const p = new Proxy( + {}, + { + has(target, prop) { + console.log(`called: ${prop}`); + return true; + }, + } +); -console.log('a' in p); // "called: a" - // true +console.log("a" in p); +// "called: a" +// true ``` The following code violates an invariant. @@ -93,7 +97,7 @@ const p = new Proxy(obj, { }, }); -'a' in p; // TypeError is thrown +"a" in p; // TypeError is thrown ``` ## Specifications diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/index.md index d3bf598fdbe7fe7..8a8a0f1a03078ed 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/index.md @@ -92,7 +92,7 @@ In this example the target has two properties, `notProxied` and ```js const target = { notProxied: "original value", - proxied: "original value" + proxied: "original value", }; const handler = { @@ -101,13 +101,13 @@ const handler = { return "replaced value"; } return Reflect.get(...arguments); - } + }, }; const proxy = new Proxy(target, handler); console.log(proxy.notProxied); // "original value" -console.log(proxy.proxied); // "replaced value" +console.log(proxy.proxied); // "replaced value" ``` ## Specifications diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/isextensible/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/isextensible/index.md index cc2405be4709e65..30a0a6b9b910119 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/isextensible/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/isextensible/index.md @@ -64,25 +64,32 @@ If the following invariants are violated, the proxy will throw a The following code traps {{jsxref("Object.isExtensible()")}}. ```js -const p = new Proxy({}, { - isExtensible(target) { - console.log('called'); - return true; +const p = new Proxy( + {}, + { + isExtensible(target) { + console.log("called"); + return true; + }, } -}); +); -console.log(Object.isExtensible(p)); // "called" - // true +console.log(Object.isExtensible(p)); +// "called" +// true ``` The following code violates the invariant. ```js example-bad -const p = new Proxy({}, { - isExtensible(target) { - return false; +const p = new Proxy( + {}, + { + isExtensible(target) { + return false; + }, } -}); +); Object.isExtensible(p); // TypeError is thrown ``` diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/ownkeys/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/ownkeys/index.md index 651da088d57e519..bb9f6158e6d54ac 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/ownkeys/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/ownkeys/index.md @@ -71,31 +71,35 @@ If the following invariants are violated, the proxy will throw a The following code traps {{jsxref("Object.getOwnPropertyNames()")}}. ```js -const p = new Proxy({}, { - ownKeys(target) { - console.log('called'); - return ['a', 'b', 'c']; +const p = new Proxy( + {}, + { + ownKeys(target) { + console.log("called"); + return ["a", "b", "c"]; + }, } -}); +); -console.log(Object.getOwnPropertyNames(p)); // "called" - // [ 'a', 'b', 'c' ] +console.log(Object.getOwnPropertyNames(p)); +// "called" +// [ 'a', 'b', 'c' ] ``` The following code violates an invariant. ```js example-bad const obj = {}; -Object.defineProperty(obj, 'a', { +Object.defineProperty(obj, "a", { configurable: false, enumerable: true, - value: 10 } -); + value: 10, +}); const p = new Proxy(obj, { ownKeys(target) { return [123, 12.5, true, false, undefined, null, {}, []]; - } + }, }); console.log(Object.getOwnPropertyNames(p)); diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/preventextensions/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/preventextensions/index.md index 149557a69092d3e..6da9587f09c72b6 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/preventextensions/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/preventextensions/index.md @@ -61,26 +61,33 @@ If the following invariants are violated, the proxy will throw a {{jsxref("TypeE The following code traps {{jsxref("Object.preventExtensions()")}}. ```js -const p = new Proxy({}, { - preventExtensions(target) { - console.log('called'); - Object.preventExtensions(target); - return true; +const p = new Proxy( + {}, + { + preventExtensions(target) { + console.log("called"); + Object.preventExtensions(target); + return true; + }, } -}); +); -console.log(Object.preventExtensions(p)); // "called" - // false +console.log(Object.preventExtensions(p)); +// "called" +// false ``` The following code violates the invariant. ```js example-bad -const p = new Proxy({}, { - preventExtensions(target) { - return true; +const p = new Proxy( + {}, + { + preventExtensions(target) { + return true; + }, } -}); +); Object.preventExtensions(p); // TypeError is thrown ``` diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/set/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/set/index.md index ae89250ac7a7a3c..e2a1760f0d4e327 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/set/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/set/index.md @@ -92,19 +92,22 @@ If the following invariants are violated, the proxy will throw a The following code traps setting a property value. ```js -const p = new Proxy({}, { - set(target, prop, value, receiver) { - target[prop] = value; - console.log(`property set: ${prop} = ${value}`); - return true; +const p = new Proxy( + {}, + { + set(target, prop, value, receiver) { + target[prop] = value; + console.log(`property set: ${prop} = ${value}`); + return true; + }, } -}) +); -console.log('a' in p); // false +console.log("a" in p); // false -p.a = 10; // "property set: a = 10" -console.log('a' in p); // true -console.log(p.a); // 10 +p.a = 10; // "property set: a = 10" +console.log("a" in p); // true +console.log(p.a); // 10 ``` ## Specifications diff --git a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/setprototypeof/index.md b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/setprototypeof/index.md index 26de35930bac4ae..c9d93651dac988f 100644 --- a/files/en-us/web/javascript/reference/global_objects/proxy/proxy/setprototypeof/index.md +++ b/files/en-us/web/javascript/reference/global_objects/proxy/proxy/setprototypeof/index.md @@ -85,7 +85,8 @@ const handlerReturnsFalse = { }, }; -const newProto = {}, target = {}; +const newProto = {}, + target = {}; const p1 = new Proxy(target, handlerReturnsFalse); Object.setPrototypeOf(p1, newProto); // throws a TypeError @@ -101,14 +102,15 @@ failure, or you want to throw a custom exception value. ```js const handlerThrows = { setPrototypeOf(target, newProto) { - throw new Error('custom error'); + throw new Error("custom error"); }, }; -const newProto = {}, target = {}; +const newProto = {}, + target = {}; const p2 = new Proxy(target, handlerThrows); -Object.setPrototypeOf(p2, newProto); // throws new Error("custom error") +Object.setPrototypeOf(p2, newProto); // throws new Error("custom error") Reflect.setPrototypeOf(p2, newProto); // throws new Error("custom error") ``` diff --git a/files/en-us/web/javascript/reference/global_objects/rangeerror/index.md b/files/en-us/web/javascript/reference/global_objects/rangeerror/index.md index f931c2d507848fc..f549860d5e1c75a 100644 --- a/files/en-us/web/javascript/reference/global_objects/rangeerror/index.md +++ b/files/en-us/web/javascript/reference/global_objects/rangeerror/index.md @@ -54,7 +54,7 @@ This can be encountered when: ```js function check(n) { if (!(n >= -500 && n <= 500)) { - throw new RangeError("The argument must be between -500 and 500.") + throw new RangeError("The argument must be between -500 and 500."); } } @@ -72,7 +72,9 @@ try { ```js function check(value) { if (!["apple", "banana", "carrot"].includes(value)) { - throw new RangeError('The argument must be an "apple", "banana", or "carrot".') + throw new RangeError( + 'The argument must be an "apple", "banana", or "carrot".' + ); } } diff --git a/files/en-us/web/javascript/reference/global_objects/rangeerror/rangeerror/index.md b/files/en-us/web/javascript/reference/global_objects/rangeerror/rangeerror/index.md index f9eba9095f81021..82e674e25d67bd1 100644 --- a/files/en-us/web/javascript/reference/global_objects/rangeerror/rangeerror/index.md +++ b/files/en-us/web/javascript/reference/global_objects/rangeerror/rangeerror/index.md @@ -70,7 +70,9 @@ try { ```js function check(value) { if (!["apple", "banana", "carrot"].includes(value)) { - throw new RangeError('The argument must be an "apple", "banana", or "carrot".'); + throw new RangeError( + 'The argument must be an "apple", "banana", or "carrot".' + ); } } diff --git a/files/en-us/web/javascript/reference/global_objects/reflect/apply/index.md b/files/en-us/web/javascript/reference/global_objects/reflect/apply/index.md index 3eaadfcf0e680b7..26ecd77239a4a53 100644 --- a/files/en-us/web/javascript/reference/global_objects/reflect/apply/index.md +++ b/files/en-us/web/javascript/reference/global_objects/reflect/apply/index.md @@ -64,13 +64,13 @@ With `Reflect.apply()` this becomes less verbose and easier to understand. Reflect.apply(Math.floor, undefined, [1.75]); // 1; -Reflect.apply(String.fromCharCode, undefined, [104, 101, 108, 108, 111]) +Reflect.apply(String.fromCharCode, undefined, [104, 101, 108, 108, 111]); // "hello" -Reflect.apply(RegExp.prototype.exec, /ab/, ['confabulation']).index +Reflect.apply(RegExp.prototype.exec, /ab/, ["confabulation"]).index; // 4 -Reflect.apply(''.charAt, 'ponies', [3]) +Reflect.apply("".charAt, "ponies", [3]); // "i" ``` diff --git a/files/en-us/web/javascript/reference/global_objects/reflect/construct/index.md b/files/en-us/web/javascript/reference/global_objects/reflect/construct/index.md index 58d3265407fde13..1e165e3b854587b 100644 --- a/files/en-us/web/javascript/reference/global_objects/reflect/construct/index.md +++ b/files/en-us/web/javascript/reference/global_objects/reflect/construct/index.md @@ -70,11 +70,11 @@ an arbitrary combination of constructor and prototype by using ```js function OneClass() { - this.name = 'one'; + this.name = "one"; } function OtherClass() { - this.name = 'other'; + this.name = "other"; } // Calling this: @@ -93,7 +93,6 @@ console.log(obj2 instanceof OneClass); // false console.log(obj1 instanceof OtherClass); // true console.log(obj2 instanceof OtherClass); // true - // Another example to demonstrate below: function func1(a, b, c, d) { @@ -104,7 +103,7 @@ function func2(d, e, f, g) { console.log(arguments[3]); } -const obj1 = Reflect.construct(func1, ['I', 'Love', 'my', 'country']); +const obj1 = Reflect.construct(func1, ["I", "Love", "my", "country"]); ``` However, while the end result is the same, there is one important difference in the @@ -119,11 +118,11 @@ parameter if supplied, or `target` if not. ```js function OneClass() { - console.log('OneClass'); + console.log("OneClass"); console.log(new.target); } function OtherClass() { - console.log('OtherClass'); + console.log("OtherClass"); console.log(new.target); } @@ -150,8 +149,8 @@ OneClass.apply(obj3, args); ```js const d = Reflect.construct(Date, [1776, 6, 4]); -d instanceof Date // true -d.getFullYear() // 1776 +d instanceof Date; // true +d.getFullYear(); // 1776 ``` ## Specifications diff --git a/files/en-us/web/javascript/reference/global_objects/reflect/defineproperty/index.md b/files/en-us/web/javascript/reference/global_objects/reflect/defineproperty/index.md index bbbdbeed254ef20..71a852769104556 100644 --- a/files/en-us/web/javascript/reference/global_objects/reflect/defineproperty/index.md +++ b/files/en-us/web/javascript/reference/global_objects/reflect/defineproperty/index.md @@ -60,7 +60,7 @@ modification of a property on an object. For more details, see the ```js const obj = {}; -Reflect.defineProperty(obj, 'x', { value: 7 }); // true +Reflect.defineProperty(obj, "x", { value: 7 }); // true console.log(obj.x); // 7 ``` diff --git a/files/en-us/web/javascript/reference/global_objects/reflect/deleteproperty/index.md b/files/en-us/web/javascript/reference/global_objects/reflect/deleteproperty/index.md index 3f5cb9c90f0c699..482aa8745fa0d76 100644 --- a/files/en-us/web/javascript/reference/global_objects/reflect/deleteproperty/index.md +++ b/files/en-us/web/javascript/reference/global_objects/reflect/deleteproperty/index.md @@ -57,18 +57,18 @@ successfully deleted. It is almost identical to the non-strict ```js const obj = { x: 1, y: 2 }; -Reflect.deleteProperty(obj, 'x'); // true +Reflect.deleteProperty(obj, "x"); // true console.log(obj); // { y: 2 } const arr = [1, 2, 3, 4, 5]; -Reflect.deleteProperty(arr, '3'); // true +Reflect.deleteProperty(arr, "3"); // true console.log(arr); // [1, 2, 3, undefined, 5] // Returns true if no such property exists -Reflect.deleteProperty({}, 'foo'); // true +Reflect.deleteProperty({}, "foo"); // true // Returns false if a property is unconfigurable -Reflect.deleteProperty(Object.freeze({ foo: 1 }), 'foo'); // false +Reflect.deleteProperty(Object.freeze({ foo: 1 }), "foo"); // false ``` ## Specifications diff --git a/files/en-us/web/javascript/reference/global_objects/reflect/getownpropertydescriptor/index.md b/files/en-us/web/javascript/reference/global_objects/reflect/getownpropertydescriptor/index.md index 092874430d61d65..aa23a887fe2a7f0 100644 --- a/files/en-us/web/javascript/reference/global_objects/reflect/getownpropertydescriptor/index.md +++ b/files/en-us/web/javascript/reference/global_objects/reflect/getownpropertydescriptor/index.md @@ -56,13 +56,13 @@ of the given property if it exists in the `target` object, ### Using Reflect.getOwnPropertyDescriptor() ```js -Reflect.getOwnPropertyDescriptor({x: 'hello'}, 'x') +Reflect.getOwnPropertyDescriptor({ x: "hello" }, "x"); // {value: "hello", writable: true, enumerable: true, configurable: true} -Reflect.getOwnPropertyDescriptor({x: 'hello'}, 'y') +Reflect.getOwnPropertyDescriptor({ x: "hello" }, "y"); // undefined -Reflect.getOwnPropertyDescriptor([], 'length') +Reflect.getOwnPropertyDescriptor([], "length"); // {value: 0, writable: true, enumerable: false, configurable: false} ``` @@ -74,10 +74,10 @@ primitive), then it will cause a {{jsxref("TypeError")}}. With coerced to an object at first. ```js -Reflect.getOwnPropertyDescriptor('foo', 0) +Reflect.getOwnPropertyDescriptor("foo", 0); // TypeError: "foo" is not non-null object -Object.getOwnPropertyDescriptor('foo', 0) +Object.getOwnPropertyDescriptor("foo", 0); // { value: "f", writable: false, enumerable: true, configurable: false } ``` diff --git a/files/en-us/web/javascript/reference/global_objects/reflect/getprototypeof/index.md b/files/en-us/web/javascript/reference/global_objects/reflect/getprototypeof/index.md index 6f69756bfc9b743..634e828645cb1dd 100644 --- a/files/en-us/web/javascript/reference/global_objects/reflect/getprototypeof/index.md +++ b/files/en-us/web/javascript/reference/global_objects/reflect/getprototypeof/index.md @@ -50,28 +50,28 @@ the internal `[[Prototype]]` property) of the specified object. ### Using Reflect.getPrototypeOf() ```js -Reflect.getPrototypeOf({}) // Object.prototype -Reflect.getPrototypeOf(Object.prototype) // null -Reflect.getPrototypeOf(Object.create(null)) // null +Reflect.getPrototypeOf({}); // Object.prototype +Reflect.getPrototypeOf(Object.prototype); // null +Reflect.getPrototypeOf(Object.create(null)); // null ``` ### Compared to Object.getPrototypeOf() ```js // Same result for Objects -Object.getPrototypeOf({}) // Object.prototype -Reflect.getPrototypeOf({}) // Object.prototype +Object.getPrototypeOf({}); // Object.prototype +Reflect.getPrototypeOf({}); // Object.prototype // Both throw in ES5 for non-Objects -Object.getPrototypeOf('foo') // Throws TypeError -Reflect.getPrototypeOf('foo') // Throws TypeError +Object.getPrototypeOf("foo"); // Throws TypeError +Reflect.getPrototypeOf("foo"); // Throws TypeError // In ES2015 only Reflect throws, Object coerces non-Objects -Object.getPrototypeOf('foo') // String.prototype -Reflect.getPrototypeOf('foo') // Throws TypeError +Object.getPrototypeOf("foo"); // String.prototype +Reflect.getPrototypeOf("foo"); // Throws TypeError // To mimic the Object ES2015 behavior you need to coerce -Reflect.getPrototypeOf(Object('foo')) // String.prototype +Reflect.getPrototypeOf(Object("foo")); // String.prototype ``` ## Specifications diff --git a/files/en-us/web/javascript/reference/global_objects/reflect/index.md b/files/en-us/web/javascript/reference/global_objects/reflect/index.md index 74cc762c6918523..5d1c47b229a6edf 100644 --- a/files/en-us/web/javascript/reference/global_objects/reflect/index.md +++ b/files/en-us/web/javascript/reference/global_objects/reflect/index.md @@ -57,16 +57,16 @@ Some of these methods are also the same as corresponding methods on {{jsxref("Ob ```js const duck = { - name: 'Maurice', - color: 'white', + name: "Maurice", + color: "white", greeting() { console.log(`Quaaaack! My name is ${this.name}`); - } -} + }, +}; -Reflect.has(duck, 'color'); +Reflect.has(duck, "color"); // true -Reflect.has(duck, 'haircut'); +Reflect.has(duck, "haircut"); // false ``` @@ -80,7 +80,7 @@ Reflect.ownKeys(duck); ### Adding a new property to the object ```js -Reflect.set(duck, 'eyes', 'black'); +Reflect.set(duck, "eyes", "black"); // returns "true" if successful // "duck" now contains the property "eyes: 'black'" ``` diff --git a/files/en-us/web/javascript/reference/global_objects/reflect/isextensible/index.md b/files/en-us/web/javascript/reference/global_objects/reflect/isextensible/index.md index 250226e221785e4..8375d424f2a6a0f 100644 --- a/files/en-us/web/javascript/reference/global_objects/reflect/isextensible/index.md +++ b/files/en-us/web/javascript/reference/global_objects/reflect/isextensible/index.md @@ -45,19 +45,19 @@ See also {{jsxref("Object.isExtensible()")}}. ```js // New objects are extensible. const empty = {}; -Reflect.isExtensible(empty) // === true +Reflect.isExtensible(empty); // === true // ...but that can be changed. Reflect.preventExtensions(empty); -Reflect.isExtensible(empty) // === false +Reflect.isExtensible(empty); // === false // Sealed objects are by definition non-extensible. const sealed = Object.seal({}); -Reflect.isExtensible(sealed) // === false +Reflect.isExtensible(sealed); // === false // Frozen objects are also by definition non-extensible. const frozen = Object.freeze({}); -Reflect.isExtensible(frozen) // === false +Reflect.isExtensible(frozen); // === false ``` ### Difference with Object.isExtensible() @@ -65,10 +65,10 @@ Reflect.isExtensible(frozen) // === false If the `target` argument to this method is not an object (a primitive), then it will cause a {{jsxref("TypeError")}}. With {{jsxref("Object.isExtensible()")}}, a non-object `target` will return false without any errors. ```js -Reflect.isExtensible(1) +Reflect.isExtensible(1); // TypeError: 1 is not an object -Object.isExtensible(1) +Object.isExtensible(1); // false ``` diff --git a/files/en-us/web/javascript/reference/global_objects/reflect/preventextensions/index.md b/files/en-us/web/javascript/reference/global_objects/reflect/preventextensions/index.md index c2979ba51b902ca..fc58287d7791f1c 100644 --- a/files/en-us/web/javascript/reference/global_objects/reflect/preventextensions/index.md +++ b/files/en-us/web/javascript/reference/global_objects/reflect/preventextensions/index.md @@ -45,11 +45,11 @@ See also {{jsxref("Object.preventExtensions()")}}. ```js // Objects are extensible by default. const empty = {}; -Reflect.isExtensible(empty) // === true +Reflect.isExtensible(empty); // === true // ...but that can be changed. Reflect.preventExtensions(empty); -Reflect.isExtensible(empty) // === false +Reflect.isExtensible(empty); // === false ``` ### Difference with Object.preventExtensions() @@ -57,10 +57,10 @@ Reflect.isExtensible(empty) // === false If the `target` argument to this method is not an object (a primitive), then it will cause a {{jsxref("TypeError")}}. With {{jsxref("Object.preventExtensions()")}}, a non-object `target` will be returned as-is without any errors. ```js -Reflect.preventExtensions(1) +Reflect.preventExtensions(1); // TypeError: 1 is not an object -Object.preventExtensions(1) +Object.preventExtensions(1); // 1 ``` diff --git a/files/en-us/web/javascript/reference/global_objects/reflect/set/index.md b/files/en-us/web/javascript/reference/global_objects/reflect/set/index.md index cec6ebe213e93ff..b35149de730a302 100644 --- a/files/en-us/web/javascript/reference/global_objects/reflect/set/index.md +++ b/files/en-us/web/javascript/reference/global_objects/reflect/set/index.md @@ -57,23 +57,23 @@ property assignment and is like the ```js // Object -let obj = {} -Reflect.set(obj, 'prop', 'value') // true -obj.prop // "value" +let obj = {}; +Reflect.set(obj, "prop", "value"); // true +obj.prop; // "value" // Array -let arr = ['duck', 'duck', 'duck'] -Reflect.set(arr, 2, 'goose') // true -arr[2] // "goose" +let arr = ["duck", "duck", "duck"]; +Reflect.set(arr, 2, "goose"); // true +arr[2]; // "goose" // It can truncate an array. -Reflect.set(arr, 'length', 1) // true -arr // ["duck"] +Reflect.set(arr, "length", 1); // true +arr; // ["duck"] // With just one argument, propertyKey and value are "undefined". -let obj = {} -Reflect.set(obj) // true -Reflect.getOwnPropertyDescriptor(obj, 'undefined') +let obj = {}; +Reflect.set(obj); // true +Reflect.getOwnPropertyDescriptor(obj, "undefined"); // { value: undefined, writable: true, enumerable: true, configurable: true } ``` @@ -92,7 +92,11 @@ const receiver = {}; Reflect.set(target, "a", 2, receiver); // true // target is { a: 1 }; receiver is { a: 2 } -const target = { set a(v) { this.b = v; } }; +const target = { + set a(v) { + this.b = v; + }, +}; const receiver = {}; Reflect.set(target, "a", 2, receiver); // true // target is { a: [Setter] }; receiver is { b: 2 } diff --git a/files/en-us/web/javascript/reference/global_objects/regexp/@@match/index.md b/files/en-us/web/javascript/reference/global_objects/regexp/@@match/index.md index 61c86942b8865eb..7454f90ec2f523c 100644 --- a/files/en-us/web/javascript/reference/global_objects/regexp/@@match/index.md +++ b/files/en-us/web/javascript/reference/global_objects/regexp/@@match/index.md @@ -43,9 +43,9 @@ This method is called internally in {{jsxref("String.prototype.match()")}}. For example, the following two examples return same result. ```js -'abc'.match(/a/); +"abc".match(/a/); -/a/[Symbol.match]('abc'); +/a/[Symbol.match]("abc"); ``` If the regex is global (with the `g` flag), the regex's [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) method will be repeatedly called until `exec()` returns `null`. Otherwise, `exec()` would only be called once and its result becomes the return value of `@@match`. @@ -89,9 +89,9 @@ This method can be used in _almost_ the same way as {{jsxref("String.prototype.m ```js const re = /[0-9]+/g; -const str = '2016-01-02'; +const str = "2016-01-02"; const result = re[Symbol.match](str); -console.log(result); // ["2016", "01", "02"] +console.log(result); // ["2016", "01", "02"] ``` ### Using @@match in subclasses @@ -106,13 +106,13 @@ class MyRegExp extends RegExp { return { group(n) { return result[n]; - } + }, }; } } -const re = new MyRegExp('([0-9]+)-([0-9]+)-([0-9]+)'); -const str = '2016-01-02'; +const re = new MyRegExp("([0-9]+)-([0-9]+)-([0-9]+)"); +const str = "2016-01-02"; const result = str.match(re); // String.prototype.match calls re[@@match]. console.log(result.group(1)); // 2016 console.log(result.group(2)); // 01 diff --git a/files/en-us/web/javascript/reference/global_objects/regexp/@@replace/index.md b/files/en-us/web/javascript/reference/global_objects/regexp/@@replace/index.md index 12d99efed805242..ddb8bfa23a5bf46 100644 --- a/files/en-us/web/javascript/reference/global_objects/regexp/@@replace/index.md +++ b/files/en-us/web/javascript/reference/global_objects/regexp/@@replace/index.md @@ -42,9 +42,9 @@ A new string, with one, some, or all matches of the pattern replaced by the spec This method is called internally in {{jsxref("String.prototype.replace()")}} and {{jsxref("String.prototype.replaceAll()")}} if the `pattern` argument is a {{jsxref("RegExp")}} object. For example, the following two examples return the same result. ```js -'abc'.replace(/a/, 'A'); +"abc".replace(/a/, "A"); -/a/[Symbol.replace]('abc', 'A'); +/a/[Symbol.replace]("abc", "A"); ``` If the regex is global (with the `g` flag), the regex's [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) method will be repeatedly called until `exec()` returns `null`. Otherwise, `exec()` would only be called once. For each `exec()` result, the substitution will be prepared based on the description in [`String.prototype.replace()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#description). @@ -88,9 +88,9 @@ This method can be used in almost the same way as {{jsxref("String.prototype.rep ```js const re = /-/g; -const str = '2016-01-01'; -const newstr = re[Symbol.replace](str, '.'); -console.log(newstr); // 2016.01.01 +const str = "2016-01-01"; +const newstr = re[Symbol.replace](str, "."); +console.log(newstr); // 2016.01.01 ``` ### Using @@replace in subclasses @@ -113,9 +113,9 @@ class MyRegExp extends RegExp { } } -const re = new MyRegExp('\\d', '', 3); -const str = '01234567'; -const newstr = str.replace(re, '#'); // String.prototype.replace calls re[@@replace]. +const re = new MyRegExp("\\d", "", 3); +const str = "01234567"; +const newstr = str.replace(re, "#"); // String.prototype.replace calls re[@@replace]. console.log(newstr); // ###34567 ``` diff --git a/files/en-us/web/javascript/reference/global_objects/string/replaceall/index.md b/files/en-us/web/javascript/reference/global_objects/string/replaceall/index.md index 1c70bb5c0edb20b..e88e33f1224429d 100644 --- a/files/en-us/web/javascript/reference/global_objects/string/replaceall/index.md +++ b/files/en-us/web/javascript/reference/global_objects/string/replaceall/index.md @@ -52,13 +52,14 @@ Unlike [`replace()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/ ```js function unsafeRedactName(text, name) { - return text.replace(new RegExp(name, 'g'), '[REDACTED]'); + return text.replace(new RegExp(name, "g"), "[REDACTED]"); } function safeRedactName(text, name) { - return text.replaceAll(name, '[REDACTED]'); + return text.replaceAll(name, "[REDACTED]"); } -const report = "A hacker called ha.*er used special characters in their name to breach the system."; +const report = + "A hacker called ha.*er used special characters in their name to breach the system."; console.log(unsafeRedactName(report, "ha.*er")); // "A [REDACTED]s in their name to breach the system." console.log(safeRedactName(report, "ha.*er")); // "A hacker called [REDACTED] used special characters in their name to breach the system." @@ -79,7 +80,7 @@ For more information about how regex properties (especially the [sticky](/en-US/ ### Using replaceAll() ```js -'aabbcc'.replaceAll('b', '.'); +"aabbcc".replaceAll("b", "."); // 'aa..cc' ``` @@ -88,15 +89,15 @@ For more information about how regex properties (especially the [sticky](/en-US/ When using a regular expression search value, it must be global. This won't work: ```js example-bad -'aabbcc'.replaceAll(/b/, '.'); +"aabbcc".replaceAll(/b/, "."); // TypeError: replaceAll must be called with a global RegExp ``` This will work: ```js example-good -'aabbcc'.replaceAll(/b/g, '.'); -"aa..cc" +"aabbcc".replaceAll(/b/g, "."); +("aa..cc"); ``` ## Specifications diff --git a/files/en-us/web/javascript/reference/global_objects/string/slice/index.md b/files/en-us/web/javascript/reference/global_objects/string/slice/index.md index 815c4388367d334..6e51ada345dc990 100644 --- a/files/en-us/web/javascript/reference/global_objects/string/slice/index.md +++ b/files/en-us/web/javascript/reference/global_objects/string/slice/index.md @@ -55,15 +55,15 @@ A new string containing the extracted section of the string. The following example uses `slice()` to create a new string. ```js -const str1 = 'The morning is upon us.', // the length of str1 is 23. - str2 = str1.slice(1, 8), - str3 = str1.slice(4, -2), - str4 = str1.slice(12), - str5 = str1.slice(30); -console.log(str2); // OUTPUT: he morn -console.log(str3); // OUTPUT: morning is upon u -console.log(str4); // OUTPUT: is upon us. -console.log(str5); // OUTPUT: "" +const str1 = "The morning is upon us.", // the length of str1 is 23. + str2 = str1.slice(1, 8), + str3 = str1.slice(4, -2), + str4 = str1.slice(12), + str5 = str1.slice(30); +console.log(str2); // OUTPUT: he morn +console.log(str3); // OUTPUT: morning is upon u +console.log(str4); // OUTPUT: is upon us. +console.log(str5); // OUTPUT: "" ``` ### Using slice() with negative indexes @@ -71,11 +71,11 @@ console.log(str5); // OUTPUT: "" The following example uses `slice()` with negative indexes. ```js -const str = 'The morning is upon us.'; -str.slice(-3); // returns 'us.' +const str = "The morning is upon us."; +str.slice(-3); // returns 'us.' str.slice(-3, -1); // returns 'us' -str.slice(0, -1); // returns 'The morning is upon us' -str.slice(4, -1); // returns 'morning is upon us' +str.slice(0, -1); // returns 'The morning is upon us' +str.slice(4, -1); // returns 'morning is upon us' ``` This example counts backwards from the end of the string by `11` to find the diff --git a/files/en-us/web/javascript/reference/global_objects/symbol/toprimitive/index.md b/files/en-us/web/javascript/reference/global_objects/symbol/toprimitive/index.md index 7825c59c937d7c4..0a1ee4d0de01740 100644 --- a/files/en-us/web/javascript/reference/global_objects/symbol/toprimitive/index.md +++ b/files/en-us/web/javascript/reference/global_objects/symbol/toprimitive/index.md @@ -34,25 +34,25 @@ Following example describes how `Symbol.toPrimitive` property can modify the pri ```js // An object without Symbol.toPrimitive property. const obj1 = {}; -console.log(+obj1); // NaN +console.log(+obj1); // NaN console.log(`${obj1}`); // "[object Object]" -console.log(obj1 + ''); // "[object Object]" +console.log(obj1 + ""); // "[object Object]" // An object with Symbol.toPrimitive property. const obj2 = { [Symbol.toPrimitive](hint) { - if (hint === 'number') { + if (hint === "number") { return 10; } - if (hint === 'string') { - return 'hello'; + if (hint === "string") { + return "hello"; } return true; - } + }, }; -console.log(+obj2); // 10 — hint is "number" +console.log(+obj2); // 10 — hint is "number" console.log(`${obj2}`); // "hello" — hint is "string" -console.log(obj2 + ''); // "true" — hint is "default" +console.log(obj2 + ""); // "true" — hint is "default" ``` ## Specifications diff --git a/files/en-us/web/javascript/reference/global_objects/typedarray/index.md b/files/en-us/web/javascript/reference/global_objects/typedarray/index.md index 8159bc68d81a304..27bb8dbb3ac3172 100644 --- a/files/en-us/web/javascript/reference/global_objects/typedarray/index.md +++ b/files/en-us/web/javascript/reference/global_objects/typedarray/index.md @@ -55,7 +55,7 @@ new (Object.getPrototypeOf(Int8Array))(); Instead, you create an instance of a typed array of a particular type, such as an {{jsxref("Int8Array")}} or a {{jsxref("BigInt64Array")}}. These objects all have a common syntax for their constructors: -```js +```js-nolint new TypedArray() new TypedArray(length) new TypedArray(typedArray) @@ -218,18 +218,18 @@ int16[0] = 42; console.log(int16[0]); // 42 // Indexed properties on prototypes are not consulted (Fx 25) -Int8Array.prototype[20] = 'foo'; -(new Int8Array(32))[20]; // 0 +Int8Array.prototype[20] = "foo"; +new Int8Array(32)[20]; // 0 // even when out of bound -Int8Array.prototype[20] = 'foo'; -(new Int8Array(8))[20]; // undefined +Int8Array.prototype[20] = "foo"; +new Int8Array(8)[20]; // undefined // or with negative integers -Int8Array.prototype[-1] = 'foo'; -(new Int8Array(8))[-1]; // undefined +Int8Array.prototype[-1] = "foo"; +new Int8Array(8)[-1]; // undefined // Named properties are allowed, though (Fx 30) -Int8Array.prototype.foo = 'bar'; -(new Int8Array(32)).foo; // "bar" +Int8Array.prototype.foo = "bar"; +new Int8Array(32).foo; // "bar" ``` ### Cannot be frozen diff --git a/files/en-us/web/javascript/reference/global_objects/typedarray/name/index.md b/files/en-us/web/javascript/reference/global_objects/typedarray/name/index.md index 4ce38a09bfaa283..d5173283bb94cfb 100644 --- a/files/en-us/web/javascript/reference/global_objects/typedarray/name/index.md +++ b/files/en-us/web/javascript/reference/global_objects/typedarray/name/index.md @@ -28,17 +28,17 @@ The **`TypedArray.name`** property represents a string value of the typed array ### Using name ```js -Int8Array.name; // "Int8Array" -Uint8Array.name; // "Uint8Array" +Int8Array.name; // "Int8Array" +Uint8Array.name; // "Uint8Array" Uint8ClampedArray.name; // "Uint8ClampedArray" -Int16Array.name; // "Int16Array" -Uint16Array.name; // "Uint16Array" -Int32Array.name; // "Int32Array" -Uint32Array.name; // "Uint32Array" -Float32Array.name; // "Float32Array" -Float64Array.name; // "Float64Array" -BigInt64Array.name; // "BigInt64Array" -BigUint64Array.name; // "BigUint64Array" +Int16Array.name; // "Int16Array" +Uint16Array.name; // "Uint16Array" +Int32Array.name; // "Int32Array" +Uint32Array.name; // "Uint32Array" +Float32Array.name; // "Float32Array" +Float64Array.name; // "Float64Array" +BigInt64Array.name; // "BigInt64Array" +BigUint64Array.name; // "BigUint64Array" ``` ## Specifications