Skip to content

Instantly share code, notes, and snippets.

View ferreiratiago's full-sized avatar
🌍

Tiago Ferreira ferreiratiago

🌍
View GitHub Profile
@ferreiratiago
ferreiratiago / my-generator.js
Created June 14, 2018 08:36
Generator example
function* myGenerator() {
var array = [1, 2];
while (array.length) {
yield array.shift();
}
}
var generator = myGenerator();
console.log(generator.next()); // { value: 1, done: false }
@ferreiratiago
ferreiratiago / my-iterator.js
Created June 14, 2018 07:57
Iterator example
function myIterator() {
var array = [1, 2];
return {
next: function() {
if (array.length) {
return {
value: array.shift(),
done: false
};
} else {
function myIterator() {
var array = [1, 2];
return {
next: function() {
if (array.length) {
return {
value: array.shift(),
done: false
};
} else {
typeof undefined // 'undefined'
typeof null // 'object'
({}).toString() // '[object Object]'
[].toString() // ''
({}).valueOf() // {}
[].valueOf() // []
@ferreiratiago
ferreiratiago / slice.js
Last active May 15, 2018 15:05
JS WTF
[1,2,3].slice(0, null) // []
[1,2,3].slice(0, undefined) // [1,2,3]
> 0.1.toString(2)
'0.0001100110011001100110011001100110011001100110011001101'
> 0.2.toString(2)
'0.001100110011001100110011001100110011001100110011001101'
'0'.charCodeAt(0) // 48
'2'.charCodeAt(0) // 50
'10'.charCodeAt(0) // 49
var example = { length: 3 } // An array-like object
// When executing 'Array.apply(null, example)'
// Array will be executed with its arguments being the properties' values from 0...length - 1
Array(example[0], example[1], example[2])
// Which translates into
Array(undefined, undefined, undefined) // [ undefined, undefined, undefined ]