Skip to content

Instantly share code, notes, and snippets.

@lovetingyuan
lovetingyuan / is-promise.js
Last active May 31, 2019 08:08
is it a promise? it is not a duck.
function isPromise(v) {
return v && typeof v.then === 'function' && Promise.resolve(v) === v && v instanceof Promise
}
@lovetingyuan
lovetingyuan / is-native-class.js
Created December 3, 2018 06:44
is it a native class?
function isNativeClass(v) {
return !Function.prototype.toString.call(v).indexOf('class')
}
@lovetingyuan
lovetingyuan / element-in-viewport.js
Created January 24, 2019 04:59
is element in viewport?
const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
const { top, left, bottom, right } = el.getBoundingClientRect();
const { innerHeight, innerWidth } = window;
return partiallyVisible
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
};
@lovetingyuan
lovetingyuan / run-in-worker.js
Created January 24, 2019 05:07
async run function in a worker
const runAsync = fn => {
const worker = new Worker(
URL.createObjectURL(new Blob([`postMessage((${fn})());`]), {
type: 'application/javascript; charset=utf-8'
})
);
return new Promise((res, rej) => {
worker.onmessage = ({ data }) => {
res(data), worker.terminate();
};
@lovetingyuan
lovetingyuan / smooth-scroll.js
Created January 24, 2019 05:10
scroll window or element
const scrollToTop = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
};
const smoothScroll = element =>
document.querySelector(element).scrollIntoView({
@lovetingyuan
lovetingyuan / throttle.js
Created January 24, 2019 05:15
throttle
const throttle = (fn, wait) => {
let inThrottle, lastFn, lastTime;
return function() {
const context = this,
args = arguments;
if (!inThrottle) {
fn.apply(context, args);
lastTime = Date.now();
inThrottle = true;
} else {
@lovetingyuan
lovetingyuan / return_void.js
Last active May 30, 2019 08:15
one case to use void
function doSth() {
if (foo) {
doSomething()
return
}
if (bar) {
return bar
}
}
// ----------
@lovetingyuan
lovetingyuan / isInViewport.js
Created October 11, 2019 06:41
isInViewport.js
/*!
* Determine if an element is in the viewport
* (c) 2017 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Node} elem The element
* @return {Boolean} Returns true if element is in the viewport
*/
var isInViewport = function (elem) {
var distance = elem.getBoundingClientRect();
return (
distance.top >= 0 &&
const context = {
title: 'hello',
list: [1, 2, 3]
}
const template = ' \
<header>${title}</header> \
<ul>${list.map(i => `<li>${i}</li>`).join("")}</ul> \
'
@lovetingyuan
lovetingyuan / javascript_extend.js
Created August 24, 2020 05:49
Javascript extend
function Parent (name) {
this.name = name
}
Parent.prototype.getName = function getName () { return this.name }
function Child (name, age) {
Parent.call(this, name)
this.age = age
}
Child.prototype = Object.create(Parent.prototype, {