for(var i = 0; i < 5; i++) {
setTimeout(function() {
console.log(i);
}, 0)
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/*****************NATIVE forEACH*********************/ | |
Array.prototype.myEach = function(callback) { | |
for (var i = 0; i < this.length; i++) | |
callback(this[i], i, this); | |
}; | |
//tests |
{
"Array Method": {
"prefix": "arrm",
"body": [
"${1|forEach,map,reduce,filter,some,every,find,flat|}((${2:element}, ${3:index}) => {",
"\t$4",
"})",
""
],
In production, it is recommended to minify any JavaScript code that is included with your application. Minification can help your website load several times faster, especially as the size of your JavaScript source code grows.
Here's one way to set it up:
- Install Node.js
- Run
npm init -y
in your project folder (don't skip this step!) - Run
npm install terser
Now, to minify a file called like_button.js
, run in the terminal:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div>Some external code</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
color: "Goldenrod" | |
}, | |
{ | |
color: "Green" | |
}, | |
{ | |
color: "Khaki" | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
Complete feature detection for ES modules. Covers: | |
1. Static import: import * from './foo.js'; | |
2. Dynamic import(): import('./foo.js').then(module => {...}); | |
Demo: http://jsbin.com/tilisaledu/1/edit?html,output | |
Thanks to @_gsathya, @kevincennis, @rauschma, @malyw for the help. | |
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const deepClone = obj => { | |
let clone = Object.assign({}, obj); | |
Object.keys(clone).forEach( | |
key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]) | |
); | |
return Array.isArray(obj) && obj.length | |
? (clone.length = obj.length) && Array.from(clone) | |
: Array.isArray(obj) | |
? Array.from(obj) | |
: clone; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let generateRandomNumbers = (quantity) => { | |
let randomValues = { | |
[Symbol.iterator]: function() { | |
return { | |
next: function() { | |
return { value: ~~(Math.random() * quantity) }; | |
} | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo && foo.bar && foo.bar.baz && foo.bar.baz.qux |