Skip to content

Instantly share code, notes, and snippets.

View weihanglo's full-sized avatar

Weihang Lo weihanglo

View GitHub Profile
@weihanglo
weihanglo / async-functions.md
Last active July 27, 2018 10:00
Modern Concurrency in JavaScript - Async Functions

Modern Concurrency in JavaScript - Async Functions

在前一篇介紹 [JavaScript Concurrency 的文章][weihanglo-promise]中,Promise 提供開發者安全統一的標準 API,透過 thenable 減少 callback hell,巨幅降低開發非同步程式的門檻,大大提升可維護性。不過,Promise 仍沒達到 JS 社群的目標「Write async code synchronously」。本篇文章將簡單最新的 Concurrency Solution「Async Functions」,利用同步的語法寫非同步的程式,整個人都變潮了呢!

(撰於 2017-06-17,基於 ECMAScript 7+)

Introduction

@weihanglo
weihanglo / cancellableGet.js
Created July 2, 2017 08:18
Simple cancellable GET request promise
/**
* GET request with a cancelable token
* @param {string} url - url to request
* @param {objecg} token - an object with a `cancel` function property.
* @return {Promise}
*/
export function cancellableGet (url, token) {
const xhr = new XMLHttpRequest()
return new Promise(function (resolve, reject) {
token.cancel = function () {
@weihanglo
weihanglo / getClientOffset.js
Created July 2, 2017 08:19
Get real element offset with an offsetParent referencing element
/**
* Get real element offset with an offsetParent referencing element
*
* `getBoundingClientRect` return values is not correct under CSS multi-column
* layout, so we recursively get `offsetLeft`/`offsetTop` instead.
* @param {HTMLElement} el - element of interest
* @param {HTMLElement} stopEl - one of the offsetParent as a reference
* @return {object} object wth top and left offset
*/
export function getClientOffset (el, stopEl) {
@weihanglo
weihanglo / getRenderedRect.js
Created July 13, 2017 07:08
Calculate the rendered rect of img content (useful with object-fit: contain)
/**
* Calculate the rendered rect of img content (useful with object-fit: contain)
* @param {HTMLImageElement} img
* @return {object} rect of the object (the coordinate origin is top-left).
*/
export function getRenderedRect (img) {
const style = window.getComputedStyle(img)
const pos = style.getPropertyValue('object-position').split(' ')
const contains = style.getPropertyValue('object-fit') === 'contain'
@weihanglo
weihanglo / days-with-internet-explorer.md
Last active January 11, 2022 09:39
與 IE 相處的日子

與 IE 相處的日子

近幾年來,JavaScript 可謂風生水起,從後端到前端,從 mobile 到 desktop,各種 module 滿天飛,信手拈來就是一個 web app。不過,「沒碰過 IE,別說你會做前端」,本人從超新手的角度出發,整理最近修正 IE 相容性遇到的坑與解法,給自己日後留個參考。

(撰於 2017-07-15,基於 IE 11/Edge 15)

Contents

@weihanglo
weihanglo / answers-to-cherny-interview-questions.md
Last active March 2, 2021 06:47
Answers to Cherny's Interview Questions

Answers to Cherny's Interview Questions

這份面試題出自[於此][questions-source],是從 [/r/Frontend/][reddit-frontend] 連結過去的,看到如此自豪的標題和簡介,便手癢來作答。結果寫完基礎概念篇,才發現這份題目在 reddit 上被批評到體無完膚,與現代前端技術棧相差頗大。不過,一些核心概念還是挺重要的,在此分享小弟的答案,有任何錯誤,請各位不吝賜教。

(撰於 2017-07-26)

Concepts

@weihanglo
weihanglo / thoughts-on-react-native-from-an-ios-developer.md
Last active August 2, 2023 16:51
Thoughts on React Native from an iOS developer

Thoughts on React Native from an iOS developer

React Native

About two month ago, I started making a React Native app ["PyConTW 17"][pycontw-mobile] for the biggest annual Python conference in Taiwan ([PyCon Taiwan][pycontw]). The app is quite simple, but still took some efforts for me to build. As a complete React newbie, I would like to share some of my thoughts about React Native.

(written on 2017-07-30, based on React Native 0.44.2)

@weihanglo
weihanglo / intro-rx-2-observer-pattern.md
Last active August 20, 2017 05:27
Intro Rx - 2. Observer Pattern

Intro Rx - 2. Observer Pattern

本篇介紹 Rx 另一個重要的基礎概念 Observer pattern

(撰於 2017-08-15,基於 Swift 3.1)

Definition

@weihanglo
weihanglo / intro-rx-1-iterator-pattern.md
Last active August 20, 2017 05:06
Intro Rx - 1. Iterator Pattern

Intro Rx - 1. Iterator Pattern

本篇介紹 Rx 的重要基礎概念 Iterator pattern

(撰於 2017-08-15,基於 Swift 3.1)

Definition

@weihanglo
weihanglo / intro-rx-0-reactivex.md
Last active August 22, 2017 12:52
Intro of ReactiveX

Intro of ReactiveX

聽過 Reactive Programming 嗎?ReactiveX(Rx)是近來火紅的技術,帶動函數響應式程式設計的熱潮。本系列將從 Rx 最原始的概念解釋起,一步步認識 Rx 巧妙的設計理念。期盼讀完後,人人心中都能有 Reactive 的思維!

(撰於 2017-08-15)