Skip to content

Instantly share code, notes, and snippets.

View LiuuY's full-sized avatar
💭
👨‍🚀☕️

LiuuY

💭
👨‍🚀☕️
View GitHub Profile
@LiuuY
LiuuY / weakMapDebugger.js
Last active November 8, 2016 17:28
JavaScript debugging tip: use a WeakMap to put a magic __id__ property on every object, use it in logs to uniquely identify objects.
// https://twitter.com/dan_abramov/status/794655915769266178
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
let ids = new weakMap()
let nextId = 1
Object.defineProperty(Object.prototype, '__id__', {
get() {
if (!ids.has(this)) {
ids.set(this, nextId++)
}
@LiuuY
LiuuY / switch.js
Last active January 16, 2017 03:55
Switch case scope
// https://twitter.com/mxstbr/status/820228619549155328
// https://babeljs.io/repl/#?babili=false&evaluate=false&lineWrap=false&presets=es2015&code=const%20someFunc%20%3D%20(something)%20%3D%3E%20%7B%0A%20%20switch%20(something)%20%7B%0A%20%20%20%20case%20'abc'%3A%0A%20%20%20%20%20%20const%20items%20%3D%20%5B'asdf'%5D%0A%20%20%20%20%20%20return%20items%0A%20%20%20%20case%20'xyz'%3A%0A%20%20%20%20%20%20const%20items%20%3D%20%5B'bla'%5D%0A%20%20%20%20%20%20%2F%2F%20%20%20%20%E2%86%91%0A%20%20%20%20%20%20%2F%2F%20%20%20%20Babel%20complains%20here%20about%20a%0A%20%20%20%20%20%20%2F%2F%20%20%20%20'Duplicate%20declaration%20%22items%22'%0A%20%20%20%20%20%20%2F%2F%20%20%20%20(see%20below)%0A%20%20%20%20%20%20%2F%2F%20%20%20%20Why%3F%3F%0A%20%20%20%20%20%20return%20items%0A%20%20%7D%0A%7D
const someFunc = (something) => {
switch (something) {
case 'abc':
const items = ['asdf']
return items
case 'xyz':
const items = ['bla']
@LiuuY
LiuuY / index.js
Created January 28, 2017 13:13
How to get the background color of an element using javascript
function getBgColor(e, pseudo) {
var transparent = 'rgba(0, 0, 0, 0)'
if (!e) {
return transparent
}
var currentBgColor = window.getComputedStyle(e, pseudo).backgroundColor
if (currentBgColor === transparent) {
getBgColor(e.parentElement)