Skip to content

Instantly share code, notes, and snippets.

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

LiuuY

💭
👨‍🚀☕️
View GitHub Profile
@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)
@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 / 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 / Preferences.sublime-settings
Created October 28, 2016 07:59
Sublime Settings
{
"color_scheme": "Packages/User/SublimeLinter/Oceanic Next (SL).tmTheme",
"font_face": "Inziu Iosevka SC",
"font_size": 12,
"ignored_packages":
[
"Vintage"
],
"line_padding_bottom": 1,
"line_padding_top": 1,

Thanks everyone for participating in the quiz!
Many of you have posted correct answers.

What we know:

A top-level App component returns <Button /> from its render() method.

Question:

>What is the relationship between `` and this in that `Button`’s `render()`?

@LiuuY
LiuuY / index.html
Created September 30, 2016 02:29
Naïve React (no updates 😛) in 60 lines https://twitter.com/dan_abramov/status/781620845185732608
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="root"></div>
</body>
/**
* Zebra striped text lines
*/
pre {
padding: .5em;
line-height: 1.5;
background: hsl(20, 50%, 95%);
background-image: linear-gradient(
rgba(120,0,0,.1) 50%, transparent 0);
@LiuuY
LiuuY / validBraces.js
Created July 29, 2016 07:42
Valid Braces
function validBraces(braces){
var matches = { '(':')', '{':'}', '[':']' };
var stack = [];
var currentChar;
for (var i = 0; i < braces.length; i++) {
currentChar = braces[i];
if (matches[currentChar]) { // opening braces
stack.push(currentChar);
@LiuuY
LiuuY / arrayInJSX.jsx
Created July 29, 2016 07:17
Array in jsx
// {[...Array(10)].map((x, i) =>
// <div></div>
// )}
var Hello = React.createClass({
getInitialState() {
return {curr: 0}
},
shouldComponentUpdate(nextProps, nextState) {
return !(nextState.curr === this.state.curr)
@LiuuY
LiuuY / Code Structure
Last active July 29, 2016 07:03
Code Structure
现阶段的代码组织结构是依据代码的类型:
例如action文件夹、pages文件夹等等,但是这样不利于代码的重用,因为一个模块,包含redux相关,css,jsx等等,分布在不同的文件夹里面:
asssets
|- css
|- actions
| - login.js
|- reducers
| - login.js
|- pages
| - login