Skip to content

Instantly share code, notes, and snippets.

@yy-dev7
yy-dev7 / getSingle.js
Created March 20, 2017 09:09
getSingle
/**
* [惰性单例]
* @param {Function} fn [需要使用单例模式的函数]
* @return {Function}
*/
function getSingle(fn) {
var ret
return (...args) => ret || (ret = fn.apply(this, args))
}
@yy-dev7
yy-dev7 / Cookies.js
Created March 20, 2017 10:59
Cookies
const Cookies = {
get(name) {
let start
let end
if (document.cookie.length > 0) {
start = document.cookie.indexOf(`${name}=`)
if (start !== -1) {
start = start + name.length + 1
end = document.cookie.indexOf(';', start)
@yy-dev7
yy-dev7 / cssSupported.js
Created March 20, 2017 11:04
cssSupported
/**
* 检测浏览器是否支持某个css属性
* @param {[String]} prop [属性名]
* @param {[String]} value [属性值]
* @return {[Boolean]} true:支持 否则不支持
*/
function cssSupported(prop, value) {
const d = document.createElement('div')
d.style[prop] = value
return d.style[prop] === value
@yy-dev7
yy-dev7 / generateUUID.js
Created March 20, 2017 11:05
generateUUID
/**
* 生成uuid
* @return {[string]} uuid
*/
function generateUUID() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)
}
@yy-dev7
yy-dev7 / scrollfix.js
Created March 20, 2017 11:23
ScrollFix
/**
* ScrollFix v0.1
* http://www.joelambert.co.uk
*
* Copyright 2011, Joe Lambert.
* Free to use under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
var ScrollFix = function(elem) {
@yy-dev7
yy-dev7 / cacheProxyFactory.js
Created March 21, 2017 01:33
cacheProxyFactory
function cacheProxyFactory(fn) {
var cache = {}
return function() {
var args = [].join.call(arguments, ',')
if (args in cache) {
return cache[args]
}
return cache[args] = fn.apply(this, arguments)
}
}
@yy-dev7
yy-dev7 / synchronousFile.js
Created March 21, 2017 01:35
proxySynchronousFile
var synchronousFile = function(id) {
console.log('开始同步文件,id 为: ' + id);
}
// 虚拟代理http请求
var proxySynchronousFile = (function() {
var cache = [], timer
return function(id) {
cache.push(id)
@yy-dev7
yy-dev7 / EventEmitter.js
Last active January 9, 2018 04:03
Observer
class EventEmitter {
constructor() {
this.clientList = {}
}
on(key, fn) {
if (!this.clientList[key]) {
this.clientList[key] = []
}
this.clientList[key].push(fn)
function optionalChaining(obj, chain) {
return chain
.split('.')
.reduce(function(acc, val) {
return acc ? acc[val] : undefined;
}, obj);
}
var user = { address: { street: 'No.969 West WenYi Road', }, a: { b: { c: 2 } }, }
var ret = optionalChaining(user, 'address');
console.log(ret)
@yy-dev7
yy-dev7 / scrollToY.js
Last active June 19, 2017 06:57
Y轴滑动
window.requestAnimFrame = (function requestAnimFrame() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function simulation(callback) {
window.setTimeout(callback, 1000 / 60)
}
}())
function scrollToY(scrollTargetY = 0, speed = 2000, easing = 'easeOutSine') {