├── CONTRIBUTING.md
├── CONTRIBUTING_es_ES.md
├── CONTRIBUTING_zh_CN.md
├── CONTRIBUTING_zh_TW.md
├── LICENSE.md
├── POST_TEMPLATE.md
├── PULL_REQUEST_TEMPLATE.md
├── README.md
└── _posts
├── en
├── angular
│ ├── 2016-01-01-angularjs-digest-vs-apply.md
│ └── 2017-03-07-preventing-unwanted-scopes-creation-in-angularjs.md
├── javascript
│ ├── 2015-12-29-insert-item-inside-an-array.md
│ ├── 2016-01-03-improve-nested-conditionals.md
│ ├── 2016-01-04-sorting-strings-with-accented-characters.md
│ ├── 2016-01-05-differences-between-undefined-and-null.md
│ ├── 2016-01-06-writing-a-single-method-for-arrays-and-a-single-element.md
│ ├── 2016-01-07-use-strict-and-get-lazy.md
│ ├── 2016-01-08-converting-a-node-list-to-an-array.md
│ ├── 2016-01-09-template-strings.md
│ ├── 2016-01-10-check-if-a-property-is-in-a-object.md
│ ├── 2016-01-11-hoisting.md
│ ├── 2016-01-12-pseudomandatory-parameters-in-es6-functions.md
│ ├── 2016-01-13-tip-to-measure-performance-of-a-javascript-block.md
│ ├── 2016-01-14-fat-arrow-functions.md
│ ├── 2016-01-15-even-simpler-way-of-using-indexof-as-a-contains-clause.md
│ ├── 2016-01-16-passing-arguments-to-callback-functions.md
│ ├── 2016-01-17-nodejs-run-a-module-if-it-is-not-required.md
│ ├── 2016-01-18-rounding-the-fast-way.md
│ ├── 2016-01-19-safe-string-concatenation.md
│ ├── 2016-01-20-return-objects-to-enable-chaining-of-functions.md
│ ├── 2016-01-21-shuffle-an-array.md
│ ├── 2016-01-22-two-ways-to-empty-an-array.md
│ ├── 2016-01-23-converting-to-number-fast-way.md
│ ├── 2016-01-24-use_===_instead_of_==.md
│ ├── 2016-01-25-Using-immediately-invoked-function-expression.md
│ ├── 2016-01-26-filtering-and-sorting-a-list-of-strings.md
│ ├── 2016-01-27-short-circuit-evaluation-in-js.md
│ ├── 2016-01-28-curry-vs-partial-application.md
│ ├── 2016-01-29-speed-up-recursive-functions-with-memoization.md
│ ├── 2016-01-30-converting-truthy-falsy-values-to-boolean.md
│ ├── 2016-01-31-avoid-modifying-or-passing-arguments-into-other-functions—it-kills-optimization.md
│ ├── 2016-02-01-map-to-the-rescue-adding-order-to-object-properties.md
│ ├── 2016-02-02-create-range-0-n-easily-using-one-line.md
│ ├── 2016-02-03-implementing-asynchronous-loops.md
│ ├── 2016-02-04-assignment-shorthands.md
│ ├── 2016-02-05-observe-dom-changes.md
│ ├── 2016-02-06-deduplicate-an-array.md
│ ├── 2016-02-07-flattening-multidimensional-arrays-in-javascript.md
│ ├── 2016-02-08-advanced-properties.md
│ ├── 2016-02-09-using-json-stringify.md
│ ├── 2016-02-10-array-average-and-median.md
│ ├── 2016-02-11-preventing-unapply-attacks.md
│ ├── 2016-02-12-use-destructuring-in-function-parameters.md
│ ├── 2016-02-13-know-the-passing-mechanism.md
│ ├── 2016-02-14-calculate-the-max-min-value-from-an-array.md
│ ├── 2016-02-15-detect-document-ready-in-pure-js.md
│ ├── 2016-02-16-basics-declarations.md
│ ├── 2016-02-17-reminders-about-reduce-function-usage.md
│ ├── 2016-02-26-extract-unix-timestamp-easily.md
│ ├── 2016-03-03-helpful-console-log-hacks.md
│ ├── 2016-03-16-DOM-event-listening-made-easy.md
│ ├── 2016-04-05-return-values-with-the-new-operator.md
│ ├── 2016-04-21-get-file-extension.md
│ ├── 2016-05-06-use-optional-arguments.md
│ ├── 2016-05-12-make-easy-loop-on-array.md
│ ├── 2016-08-02-copy-to-clipboard.md
│ ├── 2016-08-10-comma-operaton-in-js.md
│ ├── 2016-08-17-break-continue-loop-functional.md
│ ├── 2016-08-25-keyword-var-vs-let.md
│ ├── 2016-10-28-three-useful-hacks.md
│ ├── 2017-01-19-binding-objects-to-functions.md
│ ├── 2017-03-09-working-with-websocket-timeout.md
│ ├── 2017-03-12-3-array-hacks.md
│ ├── 2017-03-16-tapping-for-quick-debugging.md
│ ├── 2017-03-29-recursion-iteration-and-tail-calls-in-js.md
│ ├── 2017-04-03-why-you-should-use-Object.is()-in-equality-comparison.md
│ ├── 2017-04-05-picking-and-rejecting-object-properties.md
│ ├── 2017-04-11-protocols-for-the-brave.md
│ ├── 2017-04-24-improving-your-async-functions-with-webworkers.md
│ ├── 2017-06-14-closures-inside-loops.md
│ ├── 2017-06-14-immutable-structures-and-cloning.md
│ ├── 2017-06-15-looping-over-arrays.md
│ ├── 2017-09-01-hash-maps-without-side-effects.md
│ ├── 2018-11-25-creating-immutable-objects-in-native-javascript.md
│ ├── 2020-10-13-what-is-a-functional-inheritance.md
│ ├── 2020-10-15-what-is-a-currying-function.md
│ ├── 2020-10-20-what-is-the-temporal-dead-zone.md
│ ├── 2020-10-22-difference-between-target-and-currentTarget.md
│ ├── 2020-10-27-what-is-a-spread-operator.md
│ ├── 2020-11-04-what-is-a-void-operator.md
│ ├── 2020-11-17-what-is-the-promise-executor.md
│ └── 2021-02-02-what-is-the-javascript-ternary-operator.md
├── more
│ └── 2017-04-06-vuejs-how-vuejs-makes-a-copy-update-replace-inside-the-data-binding.md
└── react
│ ├── 2016-01-02-keys-in-children-components-are-important.md
│ ├── 2017-03-27-state-to-props-maps-with-memory.md
│ ├── 2017-04-04-enhancing-react-components-composition.md
│ ├── 2017-04-10-adventurers-guide-to-react.md
│ ├── 2017-05-29-upping-performance-by-appending-keying.md
│ └── 2021-07-18-trace-the-reason-make-your-page-rerender.md
├── es_ES
├── angular
│ └── 2016-01-01-angularjs-digest-vs-apply.md
├── javascript
│ ├── 2015-12-29-insert-item-inside-an-array.md
│ ├── 2016-01-03-improve-nested-conditionals.md
│ ├── 2016-01-04-sorting-strings-with-accented-characters.md
│ ├── 2016-01-05-differences-between-undefined-and-null.md
│ ├── 2016-01-06-writing-a-single-method-for-arrays-and-a-single-element.md
│ ├── 2016-01-07-use-strict-and-get-lazy.md
│ ├── 2016-01-08-converting-a-node-list-to-an-array.md
│ ├── 2016-01-09-template-strings.md
│ ├── 2016-01-10-check-if-a-property-is-in-a-object.md
│ ├── 2016-01-11-hoisting.md
│ ├── 2016-01-12-pseudomandatory-parameters-in-es6-functions.md
│ ├── 2016-01-13-tip-to-measure-performance-of-a-javascript-block.md
│ ├── 2016-01-14-fat-arrow-functions.md
│ ├── 2016-01-15-even-simpler-way-of-using-indexof-as-a-contains-clause.md
│ ├── 2016-01-16-passing-arguments-to-callback-functions.md
│ ├── 2016-01-17-nodejs-run-a-module-if-it-is-not-required.md
│ ├── 2016-01-18-rounding-the-fast-way.md
│ ├── 2016-01-19-safe-string-concatenation.md
│ ├── 2016-01-20-return-objects-to-enable-chaining-of-functions.md
│ ├── 2016-01-21-shuffle-an-array.md
│ ├── 2016-01-22-two-ways-to-empty-an-array.md
│ ├── 2016-01-23-converting-to-number-fast-way.md
│ ├── 2016-01-24-use_===_instead_of_==.md
│ ├── 2016-01-25-Using-immediately-invoked-function-expression.md
│ ├── 2016-01-26-filtering-and-sorting-a-list-of-strings.md
│ ├── 2016-01-27-short-circiut-evaluation-in-js.md
│ ├── 2016-01-28-curry-vs-partial-application.md
│ ├── 2016-01-29-speed-up-recursive-functions-with-memoization.md
│ ├── 2016-01-30-converting-truthy-falsy-values-to-boolean.md
│ ├── 2016-01-31-avoid-modifying-or-passing-arguments-into-other-functions—it-kills-optimization.md
│ ├── 2016-02-01-map-to-the-rescue-adding-order-to-object-properties.md
│ ├── 2016-02-02-create-range-0-n-easily-using-one-line.md
│ ├── 2016-02-03-implementing-asynchronous-loops.md
│ ├── 2016-02-04-assignment-shorthands.md
│ ├── 2016-02-05-observe-dom-changes.md
│ ├── 2016-02-06-deduplicate-an-array.md
│ ├── 2016-02-07-flattening-multidimensional-arrays-in-javascript.md
│ ├── 2016-02-08-advanced-properties.md
│ ├── 2016-02-09-using-json-stringify.md
│ ├── 2016-02-10-array-average-and-median.md
│ ├── 2016-02-11-preventing-unapply-attacks.md
│ ├── 2016-02-12-use-destructuring-in-function-parameters.md
│ ├── 2016-02-13-know-the-passing-mechanism.md
│ ├── 2016-02-14-calculate-the-max-min-value-from-an-array.md
│ ├── 2016-02-15-detect-document-ready-in-pure-js.md
│ ├── 2016-02-16-basics-declarations.md
│ ├── 2016-02-17-reminders-about-reduce-function-usage.md
│ ├── 2016-02-26-extract-unix-timestamp-easily.md
│ ├── 2016-03-03-helpful-console-log-hacks.md
│ ├── 2016-03-16-DOM-event-listening-made-easy.md
│ ├── 2016-04-05-return-values-with-the-new-operator.md
│ ├── 2016-04-21-get-file-extension.md
│ ├── 2016-05-06-use-optional-arguments.md
│ └── 2016-05-12-make-easy-loop-on-array.md
└── react
│ └── 2016-01-02-keys-in-children-components-are-important.md
├── zh_CN
├── angular
│ └── 2016-01-01-angularjs-digest-vs-apply.md
├── javascript
│ ├── 2015-12-29-insert-item-inside-an-array.md
│ ├── 2016-01-03-improve-nested-conditionals.md
│ ├── 2016-01-04-sorting-strings-with-accented-characters.md
│ ├── 2016-01-05-differences-between-undefined-and-null.md
│ ├── 2016-01-06-writing-a-single-method-for-arrays-and-a-single-element.md
│ ├── 2016-01-07-use-strict-and-get-lazy.md
│ ├── 2016-01-08-converting-a-node-list-to-an-array.md
│ ├── 2016-01-09-template-strings.md
│ ├── 2016-01-10-check-if-a-property-is-in-a-object.md
│ ├── 2016-01-11-hoisting.md
│ ├── 2016-01-12-pseudomandatory-parameters-in-es6-functions.md
│ ├── 2016-01-13-tip-to-measure-performance-of-a-javascript-block.md
│ ├── 2016-01-14-fat-arrow-functions.md
│ ├── 2016-01-15-even-simpler-way-of-using-indexof-as-a-contains-clause.md
│ ├── 2016-01-16-passing-arguments-to-callback-functions.md
│ ├── 2016-01-17-nodejs-run-a-module-if-it-is-not-required.md
│ ├── 2016-01-18-rounding-the-fast-way.md
│ ├── 2016-01-19-safe-string-concatenation.md
│ ├── 2016-01-20-return-objects-to-enable-chaining-of-functions.md
│ ├── 2016-01-21-shuffle-an-array.md
│ ├── 2016-01-22-two-ways-to-empty-an-array.md
│ ├── 2016-01-23-converting-to-number-fast-way.md
│ ├── 2016-01-24-use_===_instead_of_==.md
│ ├── 2016-01-25-Using-immediately-invoked-function-expression.md
│ ├── 2016-01-26-filtering-and-sorting-a-list-of-strings.md
│ ├── 2016-01-27-short-circuit-evaluation-in-js.md
│ ├── 2016-01-28-curry-vs-partial-application.md
│ ├── 2016-01-29-speed-up-recursive-functions-with-memoization.md
│ ├── 2016-01-30-converting-truthy-falsy-values-to-boolean.md
│ ├── 2016-01-31-avoid-modifying-or-passing-arguments-into-other-functions—it-kills-optimization.md
│ ├── 2016-02-01-map-to-the-rescue-adding-order-to-object-properties.md
│ ├── 2016-02-02-create-range-0-n-easily-using-one-line.md
│ ├── 2016-02-03-implementing-asynchronous-loops.md
│ ├── 2016-02-04-assignment-shorthands.md
│ ├── 2016-02-05-observe-dom-changes.md
│ ├── 2016-02-06-deduplicate-an-array.md
│ ├── 2016-02-07-flattening-multidimensional-arrays-in-javascript.md
│ ├── 2016-02-08-advanced-properties.md
│ ├── 2016-02-09-using-json-stringify.md
│ ├── 2016-02-10-array-average-and-median.md
│ ├── 2016-02-11-preventing-unapply-attacks.md
│ ├── 2016-02-12-use-destructuring-in-function-parameters.md
│ ├── 2016-02-13-know-the-passing-mechanism.md
│ ├── 2016-02-14-calculate-the-max-min-value-from-an-array.md
│ ├── 2016-02-15-detect-document-ready-in-pure-js.md
│ ├── 2016-02-16-basics-declarations.md
│ ├── 2016-02-17-reminders-about-reduce-function-usage.md
│ ├── 2016-02-26-extract-unix-timestamp-easily.md
│ ├── 2016-03-03-helpful-console-log-hacks.md
│ ├── 2016-03-16-DOM-event-listening-made-easy.md
│ ├── 2016-04-05-return-values-with-the-new-operator.md
│ ├── 2016-04-21-get-file-extension.md
│ ├── 2016-05-06-use-optional-arguments.md
│ ├── 2016-05-12-make-easy-loop-on-array.md
│ ├── 2016-08-02-copy-to-clipboard.md
│ ├── 2016-08-10-comma-operaton-in-js.md
│ ├── 2016-08-17-break-continue-loop-functional.md
│ ├── 2016-08-25-keyword-var-vs-let.md
│ ├── 2016-10-28-three-useful-hacks.md
│ ├── 2017-01-19-binding-objects-to-functions.md
│ ├── 2017-03-09-working-with-websocket-timeout.md
│ ├── 2017-03-12-3-array-hacks.md
│ ├── 2017-03-16-tapping-for-quick-debugging.md
│ ├── 2017-04-03-why-you-should-use-Object.is()-in-equality-comparison.md
│ └── 2017-04-05-picking-and-rejecting-object-properties.md
├── more
│ └── 2017-04-06-vuejs-how-vuejs-makes-a-copy-update-replace-inside-the-data-binding.md
└── react
│ └── 2016-01-02-keys-in-children-components-are-important.md
└── zh_TW
├── angular
├── 2016-01-01-angularjs-digest-vs-apply.md
└── 2017-03-07-preventing-unwanted-scopes-creation-in-angularjs.md
├── javascript
├── 2015-12-29-insert-item-inside-an-array.md
├── 2016-01-03-improve-nested-conditionals.md
├── 2016-01-04-sorting-strings-with-accented-characters.md
├── 2016-01-05-differences-between-undefined-and-null.md
├── 2016-01-06-writing-a-single-method-for-arrays-and-a-single-element.md
├── 2016-01-07-use-strict-and-get-lazy.md
├── 2016-01-08-converting-a-node-list-to-an-array.md
├── 2016-01-09-template-strings.md
├── 2016-01-10-check-if-a-property-is-in-a-object.md
├── 2016-01-11-hoisting.md
├── 2016-01-12-pseudomandatory-parameters-in-es6-functions.md
├── 2016-01-13-tip-to-measure-performance-of-a-javascript-block.md
├── 2016-01-14-fat-arrow-functions.md
├── 2016-01-15-even-simpler-way-of-using-indexof-as-a-contains-clause.md
├── 2016-01-16-passing-arguments-to-callback-functions.md
├── 2016-01-17-nodejs-run-a-module-if-it-is-not-required.md
├── 2016-01-18-rounding-the-fast-way.md
├── 2016-01-19-safe-string-concatenation.md
├── 2016-01-20-return-objects-to-enable-chaining-of-functions.md
├── 2016-01-21-shuffle-an-array.md
├── 2016-01-22-two-ways-to-empty-an-array.md
├── 2016-01-23-converting-to-number-fast-way.md
├── 2016-01-24-use_===_instead_of_==.md
├── 2016-01-25-Using-immediately-invoked-function-expression.md
├── 2016-01-26-filtering-and-sorting-a-list-of-strings.md
├── 2016-01-27-short-circiut-evaluation-in-js.md
├── 2016-01-28-curry-vs-partial-application.md
├── 2016-01-29-speed-up-recursive-functions-with-memoization.md
├── 2016-01-30-converting-truthy-falsy-values-to-boolean.md
├── 2016-01-31-avoid-modifying-or-passing-arguments-into-other-functions—it-kills-optimization.md
├── 2016-02-01-map-to-the-rescue-adding-order-to-object-properties.md
├── 2016-02-02-create-range-0-n-easily-using-one-line.md
├── 2016-02-03-implementing-asynchronous-loops.md
├── 2016-02-04-assignment-shorthands.md
├── 2016-02-05-observe-dom-changes.md
├── 2016-02-06-deduplicate-an-array.md
├── 2016-02-07-flattening-multidimensional-arrays-in-javascript.md
├── 2016-02-08-advanced-properties.md
├── 2016-02-09-using-json-stringify.md
├── 2016-02-10-array-average-and-median.md
├── 2016-02-11-preventing-unapply-attacks.md
├── 2016-02-12-use-destructuring-in-function-parameters.md
├── 2016-02-13-know-the-passing-mechanism.md
├── 2016-02-14-calculate-the-max-min-value-from-an-array.md
├── 2016-02-15-detect-document-ready-in-pure-js.md
├── 2016-02-16-basics-declarations.md
├── 2016-02-17-reminders-about-reduce-function-usage.md
├── 2016-02-26-extract-unix-timestamp-easily.md
├── 2016-03-03-helpful-console-log-hacks.md
├── 2016-03-16-DOM-event-listening-made-easy.md
├── 2016-04-05-return-values-with-the-new-operator.md
├── 2016-04-21-get-file-extension.md
├── 2016-05-06-use-optional-arguments.md
├── 2016-05-12-make-easy-loop-on-array.md
├── 2016-08-02-copy-to-clipboard.md
├── 2016-08-10-comma-operaton-in-js.md
├── 2016-08-17-break-continue-loop-functional.md
├── 2016-08-25-keyword-var-vs-let.md
├── 2016-10-28-three-useful-hacks.md
├── 2017-01-19-binding-objects-to-functions.md
├── 2017-03-09-working-with-websocket-timeout.md
├── 2017-03-12-3-array-hacks.md
├── 2017-03-16-tapping-for-quick-debugging.md
├── 2017-03-29-recursion-iteration-and-tail-calls-in-js.md
├── 2017-04-03-why-you-should-use-Object.is()-in-equality-comparison.md
├── 2017-04-05-picking-and-rejecting-object-properties.md
└── 2017-06-15-looping-over-arrays.md
├── more
└── 2017-04-06-vuejs-how-vuejs-makes-a-copy-update-replace-inside-the-data-binding.md
└── react
├── 2016-01-02-keys-in-children-components-are-important.md
├── 2017-03-27-state-to-props-maps-with-memory.md
├── 2017-04-04-enhancing-react-components-composition.md
└── 2017-04-10-adventurers-guide-to-react.md
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to submit your tip
2 |
3 | To submit a tip to the list, fork the repository and add your tip in a new file in the correct folder (according language). The name of the file should be `2016-xx-xx-name-of-your-tip`.
4 |
5 | Use [this format](POST_TEMPLATE.md) when writing your tip.
6 |
7 | ### Requirements
8 |
9 | - The tip should be readable in less than two minutes
10 | - Adding links to other sites or videos that give more insight is welcome
11 | - Mark JS code with ```js
12 | - Don't mention "JavaScript" in the title (as our tips are about it anyway)
13 | - Use backticks (`) to mark code in the **title** and/or **tip-tldr** fields. _Warning_: Both values must not start with backticks!
14 | - How you can be rewarded: If you have a PayPal, Coinbase or another account, type the url on tip-writer-support. That's all! Now your readers will be able to grab you a beer for your knowledge.
15 |
16 | Once your tip is ready, [issue a pull request](https://help.github.com/articles/using-pull-requests/) with this [PR template](PULL_REQUEST_TEMPLATE.md) and your tip will be reviewed (see below).
17 |
18 | ## Notes
19 |
20 | Leave the date and the tip number as **xx**. When the PR is `ready to merge`, we will tell you the correct numbers. Please also [squash](https://davidwalsh.name/squash-commits-git) your commits.
21 |
22 | ## Tip flow
23 |
24 | **Tip proposal** ⇒ **Tip under review** ⇒ **Tip ready to merge**
25 |
26 | - When you send a tip, it has to pass the review process and while that happens, its status is `under review`.
27 | - Once the tip is reviewed by 5 people and has been given the reviewer's ship it emote (:shipit:), the tip is `ready to merge`.
28 |
29 |
30 | We are looking forward to your contribution!
31 |
--------------------------------------------------------------------------------
/CONTRIBUTING_es_ES.md:
--------------------------------------------------------------------------------
1 | # Como enviar tu tip
2 |
3 | Para agregar tu tip a la lista, debes forkear el repositorio y agregar tu tip en un nuevo archivos en el direcctorio correcto (de acuero el lenguage). El nombre del archivo deberia ser '2016-xx-xx-nombre-de-tu-tip'.
4 |
5 | Utilice [este formato](https://github.com/loverajoel/jstips/blob/gh-pages/POST_TEMPLATE.md) para escribir su tip.
6 |
7 | ### Requisitos
8 |
9 | - Su tip debe ser legible en menos de dos minutos.
10 | - Puede añadir enlaces a otros sitios o videos que dan una visión más clara es bienvenido.
11 | - Se marcará el código JS con ```js
12 | - No Mencione "JavaScript" en el título (como nuestros consejos son respecto de todos modos)
13 | - Use comillas invertidas (`) para marcar código en el campos **title** y/o **tip-tldr**. _Precaucion_: Ambos valores no deben comenzar con acentos abiertos!
14 |
15 |
16 | Una vez que su tip está listo, [issue a pull request](https://help.github.com/articles/using-pull-requests/) con esta [PR template](https://github.com/loverajoel/jstips/blob/gh-pages/PULL_REQUEST_TEMPLATE.md) y su tip será revisado (véase más adelante).
17 |
18 | # Notas
19 |
20 | Deje la fecha y el número de tip con **xx**. Cuando el PR es `ready to merge`, le diremos los números correctos. Por favor, también [squash] (https://davidwalsh.name/squash-commits-git) sus confirmaciones.
21 |
22 | # Flujo del Tip
23 |
24 | **Tip proposal** ⇒ **Tip under review** ⇒ **Tip ready to merge**
25 |
26 | - Cuando se envía un tip, tiene que pasar el proceso de revisión y mientras eso sucede, su estado es `under review`.
27 | - Después de que el tip sea revisado por 5 personas y han dado (:shipit:), el tip esta `ready to merge`.
--------------------------------------------------------------------------------
/CONTRIBUTING_zh_CN.md:
--------------------------------------------------------------------------------
1 | # 如何提交小知识
2 |
3 | 将小知识提交到本列表,`fork`此仓库(repository)然后将的小知识放到对应文件夹(根据语言)的新文件中。新文件的名字应该像`2016-xx-xx-name-of-your-tip`这样。
4 |
5 | 书写小知识时请按照[这个格式](https://github.com/loverajoel/jstips/blob/gh-pages/POST_TEMPLATE.md)。
6 |
7 | ### 要求
8 | - 小知识可以在两分钟内阅读完。
9 | - 增加其他网站或视频的链接提供更深讲解更佳
10 | - 使用 ```js 标记JS代码
11 | - 无序在标题处提及"JavaScript"(因为我们的小知识全是关于它的)
12 | - 使用反引号(`) 标记**标题**或**tip-tldr**中的代码 ‐ _警告_:它们都不能以反引号开头
13 |
14 | 当你的小知识准备就绪了, 根据这个[PR 模板](https://github.com/loverajoel/jstips/blob/gh-pages/GIT_TEMPLATE.md),[发布一个pull request](https://help.github.com/articles/using-pull-requests/)你的小知识将被审查(见下文)。
15 |
16 | # 提示
17 |
18 | 将时间和小知识number保留为**xx**。当PR为`ready to merge`状态时,我们将告诉你正确的数字。请同时[squash](https://davidwalsh.name/squash-commits-git)你的commits.
19 |
20 | # 工作流程
21 |
22 | **Tip 提交** ⇒ **Tip 审查** ⇒ **Tip 通过并发布**
23 |
24 | - 当你提交 tip 时,如果 tip 正在通过审查,此时 tip 状态为 `under review`。
25 | - 如果 tip 经过5个人的审查,而且他們都给了 :shipit:,tip 将会被合并(`merge`)到 tip 列表中。
26 |
27 | 期待您的贡献!
--------------------------------------------------------------------------------
/CONTRIBUTING_zh_TW.md:
--------------------------------------------------------------------------------
1 | # 如何提交你的 tip
2 |
3 | 如果要提交 tip 到目錄,fork 這個儲存庫(repository)並加入你的 tip 到檔案內,放入到正確的資料夾(根據語系)。檔案名稱應該為 `2016-xx-xx-name-of-your-tip`。
4 |
5 | 當你撰寫你的 tip 時,請使用[這個格式](POST_TEMPLATE.md)。
6 |
7 | ### 要求
8 |
9 | - tip 應該至少可以在兩分鐘內讀懂。
10 | - 你可以連結到其他的網站或者是影片讓我們了解更多。
11 | - 程式碼區塊使用 ```js。
12 | - 避免在 title 提到「JavaScript」(因為我們的 tips 都是與 JavaScript 相關的)。
13 | - 使用反引號(`)來標記程式碼 - _警告_:tip **標題**和 **tip-tldr** 不要使用反引號。
14 |
15 |
16 | 當你的 tip 準備好了,依據這個 [PR 樣板](PULL_REQUEST_TEMPLATE.md),[發送一個 PR](https://help.github.com/articles/using-pull-requests/) 你的 tip 將會被校閱。每天都會有 tip 從可用的 PR 中被合併(merged)。
17 |
18 | ## 注意
19 |
20 | 使用 **xx** 為日期和 tip 的編號。當我們決定合併你的 PR 你可以把它們增加並 [squash](https://davidwalsh.name/squash-commits-git) 到你的 commits。
21 |
22 | ## Tip 工作流程
23 |
24 | **Tip 發送** ⇒ **Tip 審查** ⇒ **Tip 接受並發布**
25 |
26 | - 當你提交 tip 時,如果 tip 正在校閱流程,則 tip 狀態為 `under-review`。
27 | - 如果 tip 經過 5 位專業的人士校閱,而且他們都給了 :shipit:,tip 將會被合併(`merge`)到 tip 清單。
28 |
--------------------------------------------------------------------------------
/POST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: *post
3 |
4 | title: Demo post
5 | tip-number: xx
6 | tip-username: tip_js
7 | tip-username-profile: https://twitter.com/tips_js
8 | tip-tldr: Just a demo
9 |
10 |
11 |
12 | categories:
13 | - en
14 | ---
15 |
16 | content here
17 | ### Subtitles format
18 |
--------------------------------------------------------------------------------
/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | ## [Title here]
2 | ## TL;DR;
3 | [content here]
4 | ## Username
5 | [twitter account link or github account link ej: [@tips-js](https://twitter.com/tips_js)]
6 | ## Extra
7 | [content here]
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-05-differences-between-undefined-and-null.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Differences between `undefined` and `null`
5 | tip-number: 05
6 | tip-username: loverajoel
7 | tip-username-profile: https://github.com/loverajoel
8 | tip-tldr: Understanding the differences between `undefined` and `null`.
9 | tip-writer-support: https://www.coinbase.com/loverajoel
10 |
11 | redirect_from:
12 | - /en/differences-between-undefined-and-null/
13 |
14 | categories:
15 | - en
16 | - javascript
17 | ---
18 |
19 | - `undefined` means a variable has not been declared, or has been declared but has not yet been assigned a value
20 | - `null` is an assignment value that means "no value"
21 | - Javascript sets unassigned variables with a default value of `undefined`
22 | - Javascript never sets a value to `null`. It is used by programmers to indicate that a `var` has no value.
23 | - `undefined` is not valid in JSON while `null` is
24 | - `undefined` typeof is `undefined`
25 | - `null` typeof is an `object`. [Why?](http://www.2ality.com/2013/10/typeof-null.html)
26 | - Both are primitives
27 | - Both are [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy)
28 | (`Boolean(undefined) // false`, `Boolean(null) // false`)
29 | - You can know if a variable is [undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)
30 |
31 | ```javascript
32 | typeof variable === "undefined"
33 | ```
34 | - You can check if a variable is [null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)
35 |
36 | ```javascript
37 | variable === null
38 | ```
39 | - The **equality** operator considers them equal, but the **identity** doesn't
40 |
41 | ```javascript
42 | null == undefined // true
43 |
44 | null === undefined // false
45 | ```
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-06-writing-a-single-method-for-arrays-and-a-single-element.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Writing a single method for arrays and a single element
5 | tip-number: 06
6 | tip-username: mattfxyz
7 | tip-username-profile: https://twitter.com/mattfxyz
8 | tip-tldr: Rather than writing separate methods to handle an array and a single element parameter, write your functions so they can handle both. This is similar to how some of jQuery's functions work (`css` will modify everything matched by the selector).
9 |
10 | redirect_from:
11 | - /en/writing-a-single-method-for-arrays-and-a-single-element/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | Rather than writing separate methods to handle an array and a single element parameter, write your functions so they can handle both. This is similar to how some of jQuery's functions work (`css` will modify everything matched by the selector).
19 |
20 | You just have to concat everything into an array first. `Array.concat` will accept an array or a single element.
21 |
22 | ```javascript
23 | function printUpperCase(words) {
24 | var elements = [].concat(words || []);
25 | for (var i = 0; i < elements.length; i++) {
26 | console.log(elements[i].toUpperCase());
27 | }
28 | }
29 | ```
30 |
31 | `printUpperCase` is now ready to accept a single node or an array of nodes as its parameter. It also avoids the potential `TypeError` that would be thrown if no parameter was passed.
32 |
33 | ```javascript
34 | printUpperCase("cactus");
35 | // => CACTUS
36 | printUpperCase(["cactus", "bear", "potato"]);
37 | // => CACTUS
38 | // BEAR
39 | // POTATO
40 | ```
41 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-09-template-strings.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Template Strings
5 | tip-number: 09
6 | tip-username: JakeRawr
7 | tip-username-profile: https://github.com/JakeRawr
8 | tip-tldr: As of ES6, JS now has template strings as an alternative to the classic end quotes strings.
9 |
10 | redirect_from:
11 | - /en/template-strings/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | As of ES6, JS now has template strings as an alternative to the classic end quotes strings.
19 |
20 | Ex:
21 | Normal string
22 |
23 | ```javascript
24 | var firstName = 'Jake';
25 | var lastName = 'Rawr';
26 | console.log('My name is ' + firstName + ' ' + lastName);
27 | // My name is Jake Rawr
28 | ```
29 | Template String
30 |
31 | ```javascript
32 | var firstName = 'Jake';
33 | var lastName = 'Rawr';
34 | console.log(`My name is ${firstName} ${lastName}`);
35 | // My name is Jake Rawr
36 | ```
37 |
38 | You can do multi-line strings without `\n`, perform simple logic (ie 2+3) or even use the [ternary operator](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) inside `${}` in template strings.
39 |
40 | ```javascript
41 | var val1 = 1, val2 = 2;
42 | console.log(`${val1} is ${val1 < val2 ? 'less than': 'greater than'} ${val2}`)
43 | // 1 is less than 2
44 | ```
45 |
46 | You are also able to modify the output of template strings using a function; they are called [tagged template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings#Tagged_template_strings) for example usages of tagged template strings.
47 |
48 | You may also want to [read](https://hacks.mozilla.org/2015/05/es6-in-depth-template-strings-2) to understand template strings more.
49 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-11-hoisting.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Hoisting
5 | tip-number: 11
6 | tip-username: squizzleflip
7 | tip-username-profile: https://twitter.com/squizzleflip
8 | tip-tldr: Understanding hoisting will help you organize your function scope.
9 |
10 | redirect_from:
11 | - /en/hoisting/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | Understanding [hoisting](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting) will help you organize your function scope. Just remember, variable declarations and function definitions are hoisted to the top. Variable definitions are not, even if you declare and define a variable on the same line. Also, a variable **declaration** lets the system know that the variable exists while **definition** assigns it a value.
19 |
20 | ```javascript
21 | function doTheThing() {
22 | // ReferenceError: notDeclared is not defined
23 | console.log(notDeclared);
24 |
25 | // Outputs: undefined
26 | console.log(definedLater);
27 | var definedLater;
28 |
29 | definedLater = 'I am defined!'
30 | // Outputs: 'I am defined!'
31 | console.log(definedLater)
32 |
33 | // Outputs: undefined
34 | console.log(definedSimulateneously);
35 | var definedSimulateneously = 'I am defined!'
36 | // Outputs: 'I am defined!'
37 | console.log(definedSimulateneously)
38 |
39 | // Outputs: 'I did it!'
40 | doSomethingElse();
41 |
42 | function doSomethingElse(){
43 | console.log('I did it!');
44 | }
45 |
46 | // TypeError: undefined is not a function
47 | functionVar();
48 |
49 | var functionVar = function(){
50 | console.log('I did it!');
51 | }
52 | }
53 | ```
54 |
55 | To make things easier to read, declare all of your variables at the top of your function scope so it is clear which scope the variables are coming from. Define your variables before you need to use them. Define your functions at the bottom of your scope to keep them out of your way.
56 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-12-pseudomandatory-parameters-in-es6-functions.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Pseudomandatory parameters in ES6 functions
5 | tip-number: 12
6 | tip-username: Avraam Mavridis
7 | tip-username-profile: https://github.com/AvraamMavridis
8 | tip-tldr: In many programming languages the parameters of a function are by default mandatory and the developer has to explicitly define that a parameter is optional.
9 |
10 | redirect_from:
11 | - /en/pseudomandatory-parameters-in-es6-functions/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | In many programming languages the parameters of a function are by default mandatory and the developer has to explicitly define that a parameter is optional. In Javascript, every parameter is optional, but we can enforce this behavior without messing with the actual body of a function, taking advantage of [**es6's default values for parameters**] (http://exploringjs.com/es6/ch_parameter-handling.html#sec_parameter-default-values) feature.
19 |
20 | ```javascript
21 | const _err = function( message ){
22 | throw new Error( message );
23 | }
24 |
25 | const getSum = (a = _err('a is not defined'), b = _err('b is not defined')) => a + b
26 |
27 | getSum( 10 ) // throws Error, b is not defined
28 | getSum( undefined, 10 ) // throws Error, a is not defined
29 | ```
30 |
31 | `_err` is a function that immediately throws an Error. If no value is passed for one of the parameters, the default value is going to be used, `_err` will be called and an Error will be thrown. You can see more examples for the **default parameters feature** on [Mozilla's Developer Network ](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters)
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-13-tip-to-measure-performance-of-a-javascript-block.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Tip to measure performance of a javascript block
5 | tip-number: 13
6 | tip-username: manmadareddy
7 | tip-username-profile: https://twitter.com/manmadareddy
8 | tip-tldr: For quickly measuring performance of a javascript block, we can use the console functions like `console.time(label)` and `console.timeEnd(label)`
9 |
10 | redirect_from:
11 | - /en/tip-to-measure-performance-of-a-javascript-block/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | For quickly measuring performance of a javascript block, we can use the console functions like
19 | [`console.time(label)`](https://developer.chrome.com/devtools/docs/console-api#consoletimelabel) and [`console.timeEnd(label)`](https://developer.chrome.com/devtools/docs/console-api#consoletimeendlabel)
20 |
21 | ```javascript
22 | console.time("Array initialize");
23 | var arr = new Array(100),
24 | len = arr.length,
25 | i;
26 |
27 | for (i = 0; i < len; i++) {
28 | arr[i] = new Object();
29 | };
30 | console.timeEnd("Array initialize"); // Outputs: Array initialize: 0.711ms
31 | ```
32 |
33 | More info:
34 | [Console object](https://github.com/DeveloperToolsWG/console-object),
35 | [Javascript benchmarking](https://mathiasbynens.be/notes/javascript-benchmarking)
36 |
37 | Demo: [jsfiddle](https://jsfiddle.net/meottb62/) - [codepen](http://codepen.io/anon/pen/JGJPoa) (outputs in browser console)
38 |
39 | > Note: As [Mozilla](https://developer.mozilla.org/en-US/docs/Web/API/Console/time) suggested don't use this for production sites, use it for development purposes only.
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-16-passing-arguments-to-callback-functions.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Passing arguments to callback functions
5 | tip-number: 16
6 | tip-username: minhazav
7 | tip-username-profile: https://twitter.com/minhazav
8 | tip-tldr: By default you cannot pass arguments to a callback function, but you can take advantage of the closure scope in Javascript to pass arguments to callback functions.
9 |
10 | redirect_from:
11 | - /en/passing-arguments-to-callback-functions/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | By default you cannot pass arguments to a callback function. For example:
19 |
20 | ```js
21 | function callback() {
22 | console.log('Hi human');
23 | }
24 |
25 | document.getElementById('someelem').addEventListener('click', callback);
26 |
27 | ```
28 |
29 | You can take advantage of the closure scope in Javascript to pass arguments to callback functions. Check this example:
30 |
31 | ```js
32 | function callback(a, b) {
33 | return function() {
34 | console.log('sum = ', (a+b));
35 | }
36 | }
37 |
38 | var x = 1, y = 2;
39 | document.getElementById('someelem').addEventListener('click', callback(x, y));
40 |
41 | ```
42 |
43 | ### What are closures?
44 |
45 | Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created. [Check MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) to learn more.
46 |
47 | So this way the arguments `x` and `y` are in scope of the callback function when it is called.
48 |
49 | Another method to do this is using the `bind` method. For example:
50 |
51 | ```js
52 | var alertText = function(text) {
53 | alert(text);
54 | };
55 |
56 | document.getElementById('someelem').addEventListener('click', alertText.bind(this, 'hello'));
57 | ```
58 | There is a very slight difference in performance of both methods, checkout [jsperf](http://jsperf.com/bind-vs-closure-23).
59 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-17-nodejs-run-a-module-if-it-is-not-required.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Node.js - Run a module if it is not `required`
5 | tip-number: 17
6 | tip-username: odsdq
7 | tip-username-profile: https://twitter.com/odsdq
8 | tip-tldr: In node, you can tell your program to do two different things depending on whether the code is run from `require('./something.js')` or `node something.js`. This is useful if you want to interact with one of your modules independently.
9 |
10 | redirect_from:
11 | - /en/nodejs-run-a-module-if-it-is-not-required/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | In node, you can tell your program to do two different things depending on whether the code is run from `require('./something.js')` or `node something.js`. This is useful if you want to interact with one of your modules independently.
19 |
20 | ```js
21 | if (!module.parent) {
22 | // ran with `node something.js`
23 | app.listen(8088, function() {
24 | console.log('app listening on port 8088');
25 | })
26 | } else {
27 | // used with `require('/.something.js')`
28 | module.exports = app;
29 | }
30 | ```
31 |
32 | See [the documentation for modules](https://nodejs.org/api/modules.html#modules_module_parent) for more info.
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-19-safe-string-concatenation.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Safe string concatenation
5 | tip-number: 19
6 | tip-username: gogainda
7 | tip-username-profile: https://twitter.com/gogainda
8 | tip-tldr: Suppose you have a couple of variables with unknown types and you want to concatenate them in a string. To be sure that the arithmetical operation is not be applied during concatenation, use concat
9 |
10 | redirect_from:
11 | - /en/safe-string-concatenation/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | Suppose you have a couple of variables with unknown types and you want to concatenate them in a string. To be sure that the arithmetical operation is not be applied during concatenation, use `concat`:
19 |
20 | ```javascript
21 | var one = 1;
22 | var two = 2;
23 | var three = '3';
24 |
25 | var result = ''.concat(one, two, three); //"123"
26 | ```
27 |
28 | This way of concatenting does exactly what you'd expect. In contrast, concatenation with pluses might lead to unexpected results:
29 |
30 | ```javascript
31 | var one = 1;
32 | var two = 2;
33 | var three = '3';
34 |
35 | var result = one + two + three; //"33" instead of "123"
36 | ```
37 |
38 | Speaking about performance, compared to the `join` [type](http://www.sitepoint.com/javascript-fast-string-concatenation/) of concatenation, the speed of `concat` is pretty much the same.
39 |
40 | You can read more about the `concat` function on MDN [page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat).
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-20-return-objects-to-enable-chaining-of-functions.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Return objects to enable chaining of functions
5 | tip-number: 20
6 | tip-username: WakeskaterX
7 | tip-username-profile: https://twitter.com/WakeStudio
8 | tip-tldr: When creating functions on an object in Object Oriented Javascript, returning the object in the function will enable you to chain functions together.
9 |
10 | redirect_from:
11 | - /en/return-objects-to-enable-chaining-of-functions/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | When creating functions on an object in Object Oriented Javascript, returning the object in the function will enable you to chain functions together.
19 |
20 | ```js
21 | function Person(name) {
22 | this.name = name;
23 |
24 | this.sayName = function() {
25 | console.log("Hello my name is: ", this.name);
26 | return this;
27 | };
28 |
29 | this.changeName = function(name) {
30 | this.name = name;
31 | return this;
32 | };
33 | }
34 |
35 | var person = new Person("John");
36 | person.sayName().changeName("Timmy").sayName();
37 | ```
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-21-shuffle-an-array.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Shuffle an Array
5 | tip-number: 21
6 | tip-username: 0xmtn
7 | tip-username-profile: https://github.com/0xmtn/
8 | tip-tldr: Fisher-Yates Shuffling it's an algorithm to shuffle an array.
9 |
10 | redirect_from:
11 | - /en/shuffle-an-array/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | This snippet here uses [Fisher-Yates Shuffling](https://www.wikiwand.com/en/Fisher%E2%80%93Yates_shuffle) Algorithm to shuffle a given array.
19 |
20 | ```javascript
21 | function shuffle(arr) {
22 | var i,
23 | j,
24 | temp;
25 | for (i = arr.length - 1; i > 0; i--) {
26 | j = Math.floor(Math.random() * (i + 1));
27 | temp = arr[i];
28 | arr[i] = arr[j];
29 | arr[j] = temp;
30 | }
31 | return arr;
32 | };
33 | ```
34 | An example:
35 |
36 | ```javascript
37 | var a = [1, 2, 3, 4, 5, 6, 7, 8];
38 | var b = shuffle(a);
39 | console.log(b);
40 | // [2, 7, 8, 6, 5, 3, 1, 4]
41 | ```
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-23-converting-to-number-fast-way.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Converting to number fast way
5 | tip-number: 23
6 | tip-username: sonnyt
7 | tip-username-profile: http://twitter.com/sonnyt
8 | tip-tldr: Converting strings to numbers is extremely common. The easiest and fastest way to achieve that would be using the + operator.
9 |
10 | redirect_from:
11 | - /en/converting-to-number-fast-way/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | Converting strings to numbers is extremely common. The easiest and fastest ([jsPerf](https://jsperf.com/number-vs-parseint-vs-plus/29)) way to achieve that would be using the `+` (plus) operator.
19 |
20 | ```javascript
21 | var one = '1';
22 |
23 | var numberOne = +one; // Number 1
24 | ```
25 |
26 | You can also use the `-` (minus) operator which type-converts the value into number but also negates it.
27 |
28 | ```javascript
29 | var one = '1';
30 |
31 | var negativeNumberOne = -one; // Number -1
32 | ```
33 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-24-use_===_instead_of_==.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Use === instead of ==
5 | tip-number: 24
6 | tip-username: bhaskarmelkani
7 | tip-username-profile: https://www.twitter.com/bhaskarmelkani
8 | tip-tldr: The `==` (or `!=`) operator performs an automatic type conversion if needed. The `===` (or `!==`) operator will not perform any conversion. It compares the value and the type, which could be considered faster ([jsPref](http://jsperf.com/strictcompare)) than `==`.
9 |
10 | redirect_from:
11 | - /en/use_===_instead_of_==/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | The `==` (or `!=`) operator performs an automatic type conversion if needed. The `===` (or `!==`) operator will not perform any conversion. It compares the value and the type, which could be considered faster ([jsPref](http://jsperf.com/strictcompare)) than `==`.
19 |
20 | ```js
21 | [10] == 10 // is true
22 | [10] === 10 // is false
23 |
24 | '10' == 10 // is true
25 | '10' === 10 // is false
26 |
27 | [] == 0 // is true
28 | [] === 0 // is false
29 |
30 | '' == false // is true but true == "a" is false
31 | '' === false // is false
32 |
33 | ```
34 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-25-Using-immediately-invoked-function-expression.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Using immediately invoked function expression
5 | tip-number: 25
6 | tip-username: rishantagarwal
7 | tip-username-profile: https://github.com/rishantagarwal
8 | tip-tldr: Called as "Iffy" ( IIFE - immediately invoked function expression) is an anonymous function expression that is immediately invoked and has some important uses in Javascript.
9 |
10 |
11 | redirect_from:
12 | - /en/Using-immediately-invoked-function-expression/
13 |
14 | categories:
15 | - en
16 | - javascript
17 | ---
18 |
19 | Called as "Iffy" ( IIFE - immediately invoked function expression) is an anonymous function expression that is immediately invoked and has some important uses in Javascript.
20 |
21 | ```javascript
22 |
23 | (function() {
24 | // Do something
25 | }
26 | )()
27 |
28 | ```
29 |
30 | It is an anonymous function expression that is immediately invoked, and it has some particularly important uses in JavaScript.
31 |
32 | The pair of parenthesis surrounding the anonymous function turns the anonymous function into a function expression or variable expression. So instead of a simple anonymous function in the global scope, or wherever it was defined, we now have an unnamed function expression.
33 |
34 | Similarly, we can even create a named, immediately invoked function expression:
35 |
36 | ```javascript
37 | (someNamedFunction = function(msg) {
38 | console.log(msg || "Nothing for today !!")
39 | }) (); // Output --> Nothing for today !!
40 |
41 | someNamedFunction("Javascript rocks !!"); // Output --> Javascript rocks !!
42 | someNamedFunction(); // Output --> Nothing for today !!
43 | ```
44 |
45 | For more details, check the following URL's -
46 | 1. [Link 1](https://blog.mariusschulz.com/2016/01/13/disassembling-javascripts-iife-syntax)
47 | 2. [Link 2](http://javascriptissexy.com/12-simple-yet-powerful-javascript-tips/)
48 |
49 | Performance:
50 | [jsPerf](http://jsperf.com/iife-with-call)
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-01-30-converting-truthy-falsy-values-to-boolean.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Converting truthy/falsy values to boolean
5 | tip-number: 30
6 | tip-username: hakhag
7 | tip-username-profile: https://github.com/hakhag
8 | tip-tldr: Logical operators are a core part of JavaScript, here you can see a a way you always get a true or false no matter what was given to it.
9 |
10 |
11 | redirect_from:
12 | - /en/converting-truthy-falsy-values-to-boolean/
13 |
14 | categories:
15 | - en
16 | - javascript
17 | ---
18 |
19 | You can convert a [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) or [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) value to true boolean with the `!!` operator.
20 |
21 | ```js
22 | !!"" // false
23 | !!0 // false
24 | !!null // false
25 | !!undefined // false
26 | !!NaN // false
27 |
28 | !!"hello" // true
29 | !!1 // true
30 | !!{} // true
31 | !![] // true
32 | ```
33 |
34 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-02-09-using-json-stringify.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Using JSON.Stringify
5 | tip-number: 40
6 | tip-username: vamshisuram
7 | tip-username-profile: https://github.com/vamshisuram
8 | tip-tldr: Create string from selected properties of JSON object.
9 |
10 |
11 | redirect_from:
12 | - /en/using-json-stringify/
13 |
14 | categories:
15 | - en
16 | - javascript
17 | ---
18 |
19 | Let's say there is an object with properties "prop1", "prop2", "prop3".
20 | We can pass __additional params__ to __JSON.stringify__ to selectively write properties of the object to string like:
21 |
22 | ```javascript
23 | var obj = {
24 | 'prop1': 'value1',
25 | 'prop2': 'value2',
26 | 'prop3': 'value3'
27 | };
28 |
29 | var selectedProperties = ['prop1', 'prop2'];
30 |
31 | var str = JSON.stringify(obj, selectedProperties);
32 |
33 | // str
34 | // {"prop1":"value1","prop2":"value2"}
35 |
36 | ```
37 |
38 | The __"str"__ will contain only info on selected properties only.
39 |
40 | Instead of array we can pass a function also.
41 |
42 | ```javascript
43 |
44 | function selectedProperties(key, val) {
45 | // the first val will be the entire object, key is empty string
46 | if (!key) {
47 | return val;
48 | }
49 |
50 | if (key === 'prop1' || key === 'prop2') {
51 | return val;
52 | }
53 |
54 | return;
55 | }
56 | ```
57 |
58 | The last optional param it takes is to modify the way it writes the object to string.
59 |
60 | ```javascript
61 | var str = JSON.stringify(obj, selectedProperties, '\t\t');
62 |
63 | /* str output with double tabs in every line.
64 | {
65 | "prop1": "value1",
66 | "prop2": "value2"
67 | }
68 | */
69 |
70 | ```
71 |
72 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-02-10-array-average-and-median.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Array average and median
5 | tip-number: 41
6 | tip-username: soyuka
7 | tip-username-profile: https://github.com/soyuka
8 | tip-tldr: Calculate the average and median from array values
9 |
10 |
11 | redirect_from:
12 | - /en/array-average-and-median/
13 |
14 | categories:
15 | - en
16 | - javascript
17 | ---
18 |
19 | The following examples will be based on the following array:
20 |
21 | ```javascript
22 | let values = [2, 56, 3, 41, 0, 4, 100, 23];
23 | ```
24 |
25 | To get the average, we have to sum up numbers and then divide by the number of values. Steps are:
26 | - get the array length
27 | - sum up values
28 | - get the average (`sum/length`)
29 |
30 | ```javascript
31 | let values = [2, 56, 3, 41, 0, 4, 100, 23];
32 | let sum = values.reduce((previous, current) => current += previous);
33 | let avg = sum / values.length;
34 | // avg = 28
35 | ```
36 |
37 | Or:
38 |
39 | ```javascript
40 | let values = [2, 56, 3, 41, 0, 4, 100, 23];
41 | let count = values.length;
42 | values = values.reduce((previous, current) => current += previous);
43 | values /= count;
44 | // avg = 28
45 | ```
46 |
47 | Now, to get the median steps are:
48 | - sort the array
49 | - get the arethmic mean of the middle values
50 |
51 | ```javascript
52 | let values = [2, 56, 3, 41, 0, 4, 100, 23];
53 | values.sort((a, b) => a - b);
54 | let lowMiddle = Math.floor((values.length - 1) / 2);
55 | let highMiddle = Math.ceil((values.length - 1) / 2);
56 | let median = (values[lowMiddle] + values[highMiddle]) / 2;
57 | // median = 13,5
58 | ```
59 |
60 | With a bitwise operator:
61 |
62 | ```javascript
63 | let values = [2, 56, 3, 41, 0, 4, 100, 23];
64 | values.sort((a, b) => a - b);
65 | let median = (values[(values.length - 1) >> 1] + values[values.length >> 1]) / 2
66 | // median = 13,5
67 | ```
68 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-02-14-calculate-the-max-min-value-from-an-array.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Calculate the Max/Min value from an array
5 | tip-number: 45
6 | tip-username: loverajoel
7 | tip-username-profile: https://www.twitter.com/loverajoel
8 | tip-tldr: Ways to use the built-in functions Math.max() and Math.min() with arrays of numbers
9 | tip-writer-support: https://www.coinbase.com/loverajoel
10 |
11 | redirect_from:
12 | - /en/calculate-the-max-min-value-from-an-array/
13 |
14 | categories:
15 | - en
16 | - javascript
17 | ---
18 |
19 | The built-in functions [Math.max()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max) and [Math.min()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min) find the maximum and minimum value of the arguments, respectively.
20 |
21 | ```js
22 | Math.max(1, 2, 3, 4); // 4
23 | Math.min(1, 2, 3, 4); // 1
24 | ```
25 |
26 | These functions will not work as-is with arrays of numbers. However, there are some ways around this.
27 |
28 | [`Function.prototype.apply()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) allows you to call a function with a given `this` value and an _array_ of arguments.
29 |
30 | ```js
31 | var numbers = [1, 2, 3, 4];
32 | Math.max.apply(null, numbers) // 4
33 | Math.min.apply(null, numbers) // 1
34 | ```
35 |
36 | Passing the `numbers` array as the second argument of `apply()` results in the function being called with all values in the array as parameters.
37 |
38 | A simpler, ES2015 way of accomplishing this is with the new [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator).
39 |
40 | ```js
41 | var numbers = [1, 2, 3, 4];
42 | Math.max(...numbers) // 4
43 | Math.min(...numbers) // 1
44 | ```
45 |
46 | This operator causes the values in the array to be expanded, or "spread", into the function's arguments.
47 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-02-15-detect-document-ready-in-pure-js.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Detect document ready in pure JS
5 | tip-number: 46
6 | tip-username: loverajoel
7 | tip-username-profile: https://www.twitter.com/loverajoel
8 | tip-tldr: The cross-browser way to check if the document has loaded in pure JavaScript
9 | tip-writer-support: https://www.coinbase.com/loverajoel
10 |
11 | redirect_from:
12 | - /en/detect-document-ready-in-pure-js/
13 |
14 | categories:
15 | - en
16 | - javascript
17 | ---
18 |
19 | The cross-browser way to check if the document has loaded in pure JavaScript is using [`readyState`](https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState).
20 |
21 | ```js
22 | if (document.readyState === 'complete') {
23 | // The page is fully loaded
24 | }
25 | ```
26 |
27 | You can detect when the document is ready...
28 |
29 |
30 | ```js
31 | let stateCheck = setInterval(() => {
32 | if (document.readyState === 'complete') {
33 | clearInterval(stateCheck);
34 | // document ready
35 | }
36 | }, 100);
37 | ```
38 |
39 | or with [onreadystatechange](https://developer.mozilla.org/en-US/docs/Web/Events/readystatechange)...
40 |
41 |
42 | ```js
43 | document.onreadystatechange = () => {
44 | if (document.readyState === 'complete') {
45 | // document ready
46 | }
47 | };
48 | ```
49 |
50 | Use `document.readyState === 'interactive'` to detect when the DOM is ready.
51 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-02-26-extract-unix-timestamp-easily.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Easiest way to extract unix timestamp in JS
5 | tip-number: 49
6 | tip-username: nmrony
7 | tip-username-profile: https://github.com/nmrony
8 | tip-tldr: In Javascript you can easily get the unix timestamp
9 |
10 | redirect_from:
11 | - /en/extract-unix-timestamp-easily/
12 |
13 | categories:
14 | - en
15 | - javascript
16 | ---
17 |
18 | We frequently need to calculate with unix timestamp. There are several ways to grab the timestamp. For current unix timestamp easiest and fastest way is
19 |
20 | ```js
21 | const dateTime = Date.now();
22 | const timestamp = Math.floor(dateTime / 1000);
23 | ```
24 | or
25 |
26 | ```js
27 | const dateTime = new Date().getTime();
28 | const timestamp = Math.floor(dateTime / 1000);
29 | ```
30 |
31 | To get unix timestamp of a specific date pass `YYYY-MM-DD` or `YYYY-MM-DDT00:00:00Z` as parameter of `Date` constructor. For example
32 |
33 | ```js
34 | const dateTime = new Date('2012-06-08').getTime();
35 | const timestamp = Math.floor(dateTime / 1000);
36 | ```
37 | You can just add a `+` sign also when declaring a `Date` object like below
38 |
39 | ```js
40 | const dateTime = +new Date();
41 | const timestamp = Math.floor(dateTime / 1000);
42 | ```
43 | or for specific date
44 |
45 | ```js
46 | const dateTime = +new Date('2012-06-08');
47 | const timestamp = Math.floor(dateTime / 1000);
48 | ```
49 | Under the hood the runtime calls `valueOf` method of the `Date` object. Then the unary `+` operator calls `toNumber()` with that returned value. For detailed explanation please check the following links
50 |
51 | * [Date.prototype.valueOf](http://es5.github.io/#x15.9.5.8)
52 | * [Unary + operator](http://es5.github.io/#x11.4.6)
53 | * [toNumber()](http://es5.github.io/#x9.3)
54 | * [Date Javascript MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
55 | * [Date.parse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)
56 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-08-02-copy-to-clipboard.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Copy to Clipboard
5 | tip-number: 56
6 | tip-username: loverajoel
7 | tip-username-profile: https://twitter.com/loverajoel
8 | tip-tldr: This week I had to create a common "Copy to Clipboard" button, I've never created one before and I want to share how I made it.
9 | tip-writer-support: https://www.coinbase.com/loverajoel
10 |
11 | redirect_from:
12 | - /en/copy-to-clipboard/
13 |
14 | categories:
15 | - en
16 | - javascript
17 | ---
18 |
19 | This is a simple tip, this week I had to create a common "Copy to Clipboard" button, I've never created one before and I want to share how I made it.
20 | It's easy, the bad thing is that we must add an `` with the text to be copied to the DOM. Then, we selected the content and execute the copy command with [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand).
21 | `execCommand('copy')` will copy the actual selected content.
22 |
23 | Also, this command that now is [supported](http://caniuse.com/#search=execCommand) by all the latest version of browsers, allows us to execute another system commands like `copy`, `cut`, `paste`, and make changes like fonts color, size, and much more.
24 |
25 | ```js
26 | document.querySelector('#input').select();
27 | document.execCommand('copy');
28 | ```
29 |
30 | ##### Playground
31 |
65 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2016-10-28-three-useful-hacks.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Three useful hacks
5 | tip-number: 60
6 | tip-username: leandrosimoes
7 | tip-username-profile: https://github.com/leandrosimoes
8 | tip-tldr: Three very useful and syntax sugar hacks to speed up your development.
9 |
10 |
11 | redirect_from:
12 | - /en/three-useful-hacks/
13 |
14 | categories:
15 | - en
16 | - javascript
17 | ---
18 |
19 | #### Getting array items from behind to front
20 |
21 | If you want to get the array items from behind to front, just do this:
22 |
23 | ```javascript
24 | var newArray = [1, 2, 3, 4];
25 |
26 | console.log(newArray.slice(-1)); // [4]
27 | console.log(newArray.slice(-2)); // [3, 4]
28 | console.log(newArray.slice(-3)); // [2, 3, 4]
29 | console.log(newArray.slice(-4)); // [1, 2, 3, 4]
30 | ```
31 |
32 | #### Short-circuits conditionals
33 |
34 | If you have to execute a function just if a condition is `true`, like this:
35 |
36 | ```javascript
37 | if(condition){
38 | dosomething();
39 | }
40 | ```
41 |
42 | You can use a short-circuit just like this:
43 |
44 | ```javascript
45 | condition && dosomething();
46 | ```
47 |
48 |
49 | #### Set variable default values using "||"
50 |
51 |
52 | If you have to set a default value to variables, you can simple do this:
53 |
54 | ```javascript
55 | var a;
56 |
57 | console.log(a); //undefined
58 |
59 | a = a || 'default value';
60 |
61 | console.log(a); //default value
62 |
63 | a = a || 'new value';
64 |
65 | console.log(a); //default value
66 | ```
--------------------------------------------------------------------------------
/_posts/en/javascript/2017-01-19-binding-objects-to-functions.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Binding objects to functions
5 | tip-number: 61
6 | tip-username: loverajoel
7 | tip-username-profile: https://github.com/loverajoel
8 | tip-tldr: Understanding how to use `Bind` method with objects and functions in JavaScript
9 |
10 |
11 | redirect_from:
12 | - /en/binding-objects-to-functions/
13 |
14 | categories:
15 | - en
16 | - javascript
17 | ---
18 |
19 | More than often, we need to bind an object to a function’s this object. JS uses the bind method when this is specified explicitly and we need to invoke desired method.
20 |
21 | ### Bind syntax
22 |
23 | ```js
24 | fun.bind(thisArg[, arg1[, arg2[, ...]]])
25 | ```
26 |
27 | ## Parameters
28 | **thisArg**
29 |
30 | `this` parameter value to be passed to target function while calling the `bound` function.
31 |
32 | **arg1, arg2, ...**
33 |
34 | Prepended arguments to be passed to the `bound` function while invoking the target function.
35 |
36 | **Return value**
37 |
38 | A copy of the given function along with the specified `this` value and initial arguments.
39 |
40 | ### Bind method in action in JS
41 |
42 | ```js
43 | const myCar = {
44 | brand: 'Ford',
45 | type: 'Sedan',
46 | color: 'Red'
47 | };
48 |
49 | const getBrand = function () {
50 | console.log(this.brand);
51 | };
52 |
53 | const getType = function () {
54 | console.log(this.type);
55 | };
56 |
57 | const getColor = function () {
58 | console.log(this.color);
59 | };
60 |
61 | getBrand(); // object not bind,undefined
62 |
63 | getBrand(myCar); // object not bind,undefined
64 |
65 | getType.bind(myCar)(); // Sedan
66 |
67 | let boundGetColor = getColor.bind(myCar);
68 | boundGetColor(); // Red
69 |
70 | ```
--------------------------------------------------------------------------------
/_posts/en/javascript/2017-03-09-working-with-websocket-timeout.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Working With Websocket Timeout
5 | tip-number: 63
6 | tip-username: loverajoel
7 | tip-username-profile: https://github.com/loverajoel
8 | tip-tldr: A trick to control the timeout
9 |
10 | categories:
11 | - en
12 | - javascript
13 | ---
14 |
15 | In case of established websocket connection, server or firewall could timeout and terminate the connection after a period of inactivity. To deal with this situation, we send periodic message to the server. To control the timeout we will add two functions in our code : one to make sure connection keep alive and another one to cancel the keep alive. Also we need a common ```timerID``` variable.
16 | Let’s have a look on implementation-
17 |
18 | ```js
19 | var timerID = 0;
20 | function keepAlive() {
21 | var timeout = 20000;
22 | if (webSocket.readyState == webSocket.OPEN) {
23 | webSocket.send('');
24 | }
25 | timerId = setTimeout(keepAlive, timeout);
26 | }
27 | function cancelKeepAlive() {
28 | if (timerId) {
29 | clearTimeout(timerId);
30 | }
31 | }
32 | ```
33 |
34 | Now as we have both of our desired function for the task, we will place ```keepAlive()``` function at the end of ```onOpen()``` method of websocket connection and ```cancelKeepAlive()``` function at the end of ```onClose()``` method of websocket connection.
35 |
36 | Yes! We have perfectly implemented hack for websocket timeout problem.
37 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2017-04-03-why-you-should-use-Object.is()-in-equality-comparison.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Why you should use Object.is() in equality comparison
5 | tip-number: 68
6 | tip-username: TarekAlQaddy
7 | tip-username-profile: https://github.com/TarekAlQaddy
8 | tip-tldr: A good solution for the looseness of equality comparisons in javascript
9 |
10 | categories:
11 | - en
12 | - javascript
13 | ---
14 |
15 | We all know that JavaScript is loosely typed and in some cases it fall behind specially when it comes to quality comparison with '==', comparing with '==' gives unexpected results due to whats called coercion or casting "converting one of the 2 operands to the other's type then compare".
16 |
17 | ``` javascript
18 | 0 == ' ' //true
19 | null == undefined //true
20 | [1] == true //true
21 | ```
22 |
23 | So they provided us with the triple equal operator '===' which is more strict and does not coerce operands, However comparing with '===' is not the best solution you can get:
24 |
25 | ``` javascript
26 | NaN === NaN //false
27 | ```
28 |
29 | The great news that in ES6 there is the new 'Object.is()' which is better and more precise it has the same features as '===' and moreover it behaves well in some special cases:
30 |
31 | ``` javascript
32 | Object.is(0 , ' '); //false
33 | Object.is(null, undefined); //false
34 | Object.is([1], true); //false
35 | Object.is(NaN, NaN); //true
36 | ```
37 |
38 | Mozilla team doesn't think that Object.is is "stricter" than '===', they say that we should think of how this method deal with NaN, -0 and +0 but overall I think it is now a good practice in real applications.
39 |
40 | Now this table illustrates..
41 |
42 | 
43 |
44 | ## References:
45 | [Equality comparisons and sameness](http://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness)
46 |
47 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2017-06-14-closures-inside-loops.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: Closures inside loops
5 | tip-number: 76
6 | tip-username: loverajoel
7 | tip-username-profile: https://github.com/loverajoel
8 | tip-tldr: Closure in a loop is an interesting topic and this is the tip to be a master of it
9 |
10 | categories:
11 | - en
12 | - javascript
13 | ---
14 |
15 | If you ever come across the likes of
16 |
17 | ```javascript
18 | var funcs = [];
19 | for (var i = 0; i < 3; i++) {
20 | funcs[i] = function() {
21 | console.log("i value is " + i);
22 | };
23 | }
24 |
25 | for (var k = 0; k < 3; k++) {
26 | funcs[k]();
27 | }
28 | ```
29 |
30 | You will notice that the expected output of
31 |
32 | ```
33 | i value is 0
34 | i value is 1
35 | i value is 2
36 | ```
37 |
38 | Doesn't match the actual output which will resemble
39 |
40 | ```
41 | i value is 3
42 | i value is 3
43 | i value is 3
44 | ```
45 |
46 | This is because of how the capturing mechanism of closures work and how `i` is represented internally.
47 |
48 | To solve this situation you can do as follows:
49 |
50 | ```javascript
51 | for (var i = 0; i < 3; i++) {
52 | funcs[i] = (function(value) {
53 | console.log("i value is " + i);
54 | })(i);
55 | }
56 | ```
57 |
58 | Which effectively copies i by value by handing it to our closure or
59 |
60 | ```javascript
61 | for (let i = 0; i < 3; i++) {
62 | funcs[i] = function() {
63 | console.log("i value is " + i);
64 | }
65 | }
66 | ```
67 |
68 | Where `let` scopes the variable to our `for` loop and produces a new value each iteration, thus `i` will be bound to different values on our closures as expected.
69 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2020-10-13-what-is-a-functional-inheritance.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: What is Functional Inheritance?
5 | tip-number: 75
6 | tip-username: loverajoel
7 | tip-username-profile: https://www.twitter.com/loverajoel
8 | tip-tldr: Functional inheritance is the process of inheriting features by applying an augmenting function to an object instance.
9 |
10 | categories:
11 | - en
12 | - javascript
13 | ---
14 |
15 | Functional inheritance is the process of inheriting features by applying an augmenting function to an object instance. The function supplies a closure scope which you can use to keep some data private. The augmenting function uses dynamic object extension to extend the object instance with new properties and methods.
16 |
17 | Functional mixins are composable factory functions that add properties and behaviors to objects like stations in an assembly line.
18 |
19 | ```javascript
20 | // Base object constructor function
21 | function Animal(data) {
22 | var that = {}; // Create an empty object
23 | that.name = data.name; // Add it a "name" property
24 | return that; // Return the object
25 | };
26 |
27 | // Create achild object, inheriting from the base Animal
28 | function Cat(data) {
29 | // Create the Animal object
30 | var that = Animal(data);
31 | // Extend base object
32 | that.sayHello = function() {
33 | return 'Hello, I\'m ' + that.name;
34 | };
35 | return that;
36 | };
37 |
38 | // Usage
39 | var myCat = Cat({ name: 'Rufi' });
40 | console.log(myCat.sayHello());
41 | // Output: "Hello, I'm Rufi"
42 | ```
43 |
44 |
45 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2020-10-15-what-is-a-currying-function.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: What is a currying function?
5 | tip-number: 75
6 | tip-username: loverajoel
7 | tip-username-profile: https://www.twitter.com/loverajoel
8 | tip-tldr: A currying function is a function that takes multiple arguments and turns it into a sequence of functions having only one argument at a time.
9 |
10 | categories:
11 | - en
12 | - javascript
13 | ---
14 |
15 | A currying function is a function that takes multiple arguments and turns it into a sequence of functions having only one argument at a time.
16 |
17 | In this way, an n-ary function becomes a unary function, and the last function returns the result of all the arguments together in a function.
18 |
19 |
20 | ```javascript
21 | // Normal definition
22 | function multiply(a, b, c) {
23 | return a * b * c;
24 | };
25 | console.log(multiply(1, 2, 3));
26 | // Output: 6
27 |
28 | // Simple curry function definition
29 | function multiply(a) {
30 | return (b) => {
31 | return (c) => {
32 | return a * b * c;
33 | };
34 | };
35 | };
36 | console.log(multiply(1)(2)(3));
37 | // Output: 6
38 | ```
39 |
40 | ### Further readings:
41 |
42 | - [Currying in JavaScript](https://dev.to/suprabhasupi/currying-in-javascript-1k3l)
43 | - [Lodash curry](https://lodash.com/docs/#curry)
44 | - [JavaScript currying](http://zetcode.com/javascript/currying/)
45 |
--------------------------------------------------------------------------------
/_posts/en/javascript/2020-10-20-what-is-the-temporal-dead-zone.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: What is the Temporal Dead Zone?
5 | tip-number: 76
6 | tip-username: loverajoel
7 | tip-username-profile: https://www.twitter.com/loverajoel
8 | tip-tldr: Temporal Dead Zone is a JavaScript behavior while using variables declared using `let` and `const` keywords.
9 |
10 | categories:
11 | - en
12 | - javascript
13 | ---
14 |
15 | Temporal Dead Zone is a JavaScript behavior while using variables declared using `let` and `const` keywords. Since the keywords are block-scoped, the variables declared these keywords could not be accessed before the declaration, and then you will have to witness where variables will be said to be `undefined`.
16 |
17 |
18 | ```javascript
19 | function myFunc(){
20 | console.log(greeting);
21 | var greeting = 'Hello World!'
22 | };
23 | myFunc(); // Output: undefined
24 |
25 | function myFunc() {
26 | console.log(greeting);
27 | let greeting = 'Hello World!';
28 | };
29 | myFunc(); // Output: ReferenceError: greeting is not defined
30 |
31 | function myFunc() {
32 | console.log(greeting);
33 | const greeting = 'Hello World!';
34 | }
35 | myFunc(); // Output: ReferenceError: greeting is not defined
36 | ```
--------------------------------------------------------------------------------
/_posts/en/javascript/2020-10-22-difference-between-target-and-currentTarget.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 |
4 | title: What is the difference between Target and currentTarget in the event context?
5 | tip-number: 77
6 | tip-username: loverajoel
7 | tip-username-profile: https://www.twitter.com/loverajoel
8 | tip-tldr: target refers to the element that triggers an event. currentTarget to the element that the event listener is listening on.
9 |
10 | categories:
11 | - en
12 | - javascript
13 | ---
14 | `target` refers to the DOM element that triggers an event. Otherwise, `currentTarget` refers to the DOM element that the event listener is listening on.
15 |
16 | ```html
17 |