JSHint
for JSHint
, mainly with the goal of exploring ES6 (I'm particularly proud of how the scope scans for variables are redone). During this process, I came across a few things that surprised me - mostly in ES6, but there is also something about ES3 that I have never used before.break
and continue
- this is a standard feature in modern programming languages. However, not everyone knows that cycles can be given labels and with their help interrupt any particular cycle: outer: for(var i = 0; i < 4; i++) { while(true) { continue outer; } }
break
. You have probably seen how it is used in a switch
: switch(i) { case 1: break; }
case
- the expression break
throws out of the switch
block, not case
, but for me the option with indents seems more readable. Switch switch
can also be tagged with: myswitch: switch(i) { case 1: break myswitch; }
{ { console.log(" "); } }
outer: { inner: { if (true) { break outer; } } console.log(" "); }
break
- the continue
statement is valid only inside the loop. I have never seen labels in Javascript code - most likely, because if you suddenly need to urgently exit more than one block, this is a reason to rewrite the code to a function with return
. function(a, b, c) { if (a) { if (b) { return true; } doSomething(); if (c) { return c; } } return b; }
function(a, b, c) { var returnValue = b; myBlock: if (a) { if (b) { returnValue = true; break myBlock; } doSomething(); if (c) { returnValue = c; } } return returnValue; }
function(a, b, c) { var returnValue = b; if (a) { if (b) { returnValue = true; } else { doSomething(); if (c) { returnValue = c; } } } return returnValue; }
var a; (a) = 1; assertTrue(a === 1);
function pullOutInParams({a}, [b]) { console.log(a, b); } function pullOutInLet(obj, arr) { let {a} = obj; let [b] = arr; console.log(a, b); } pullOutInParams({a: "Hello" }, ["World"]); pullOutInLet({a: "Hello" }, ["World"]);
let
, var
and const
. For an array, just write like this: var a; [a] = array;
var a; ({a} = array);
var a = { get b() { console.log("!"); } }; with(a) { { b } }
var a, b, c; (a) = 1; [b] = [2]; ({c} = { c : 3 });
var {1 : a} = { 1: true };
var {"1" : a} = { "1": true };
var myProp = "1"; var {[myProp] : a} = { [myProp]: true };
var a = "a"; var {[a] : [a]} = { a: [a] };
func(); function func() { console.log(" "); }
func(); // func , , "func " var func = function func() { console.log(" "); };
new func(); class func { constructor() { console.log(" "); } }
new func(); let func = function func() { console.log("Fine"); }
func
inside the temporary dead zone , which is a syntax error. function func(a, a) { console.log(a); } func("", ""); // ""
function func(a, a) { "use strict"; console.log(a); } func("", ""); // Chrome - SyntaxError: Strict mode function may not have duplicate parameter names
typeof
operator unsafetypeof
operator, you can safely find out if an identifier is declared, even if it is not assigned a value: if (typeof Symbol !== "undefined") { // Symbol } // , Symbol if (Symbol !== "undefined") { }
let
or const
. It’s all the fault of VSW , due to which accessing a variable before assigning it is a syntax error, even though “under the hood” the declaration of the variable still rises to the very top of the block. if (typeof Symbol !== "undefined") { // Symbol } let Symbol = true; // !
new
keyword. Basically, because arguments can be either the length of an array, or its elements: new Array(1); // [undefined] new Array(1, 2); // [1, 2]
var arr = new Array(10); for(var i = 0; i < arr.length; i++) { arr[i] = i; } console.dir(arr);
map
? var arr = new Array(10); arr = arr.map(function(item, index) { return index; }); console.dir(arr);
length
property, but does not create any elements. Therefore, you can access the property, but you cannot list the elements. And if you set the value of any element? var arr = new Array(10); arr[8] = undefined; arr = arr.map(function(item, index) { return index; }); console.dir(arr);
map
function, it checks the property of the property using the in
operator. The same behavior can be achieved using array literals: var arr = []; arr[9] = undefined; // var arr = []; arr.length = 10;
<--
. Not bad and read the rest of the blog posts.Source: https://habr.com/ru/post/261785/
All Articles