Last active
August 6, 2021 07:14
-
-
Save LucienLee/870b170d39060145aadb796309ec9797 to your computer and use it in GitHub Desktop.
JavaScript Anomalies
This file contains hidden or 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
/* Closure */ | |
const buttons = document.getElementsByClassName('three-buttons'); | |
for (var i = 0; i < 3; i++) { | |
buttons[i].addEventListener(function(){ | |
console.log(i) | |
}); | |
} | |
// Click 1st button -> 2 | |
// Click 2nd button -> 2 | |
// Click 3rd button -> 2 | |
/* Type */ | |
console.log(NaN === NaN); | |
// -> false | |
console.log([1,2,3] == [1,2,3]); | |
// -> false | |
/* Prototype */ | |
Array.prototype.push('๐'); | |
let arr = []; | |
console.log(arr.length) | |
// -> 0 | |
console.log(arr[0]) | |
// -> '๐' | |
/* Arguments is not an array*/ | |
(function test() { | |
arguments.map(arg => arg*2) | |
// -> arguments.map is not a function | |
Array.from(arguments).map(arg => arg*2) | |
})(1, 2, 3, 4) | |
/* float vs int toString */ | |
console.log(2.toString()); | |
// -> SyntaxError | |
console.log(2..toString()); | |
// -> '2' | |
/* Object use string as key */ | |
var a={}, | |
b={foo: 1}, | |
c={bar: 2}; | |
a[b]=123; | |
a[c]=456; | |
console.log(a[b]); | |
// -> 456 (a['object object'] = 456) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment