apply 、 call 、bind 三者都是用来改变函数的this对象的指向的; apply 、 call 、bind 三者第一个参数都是this要指向的对象,也就是想指定的上下文; apply 、 call 、bind 三者都可以利用后续参数传参;
bind 是返回对应函数,便于稍后调用;
/*实现一个响应式的正方形布局,第一反应*/ | |
.container{ | |
width:100%; | |
height:100vw; | |
} | |
/* | |
不过这货兼容性...只能说还行 |
//Thanks to https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/ | |
// typeof now | |
// Undefined => "undefined" | |
// Null => "object" *** fu*king mistake here *** | |
// Boolean => "boolean" | |
// Number => "number" | |
// String => "string" | |
// Object(uncallable) => "object" | |
// Array => "object" |
Get Homebrew installed on your mac if you don't already have it
Install highlight. "brew install highlight". (This brings down Lua and Boost as well)
// ES5- | |
if(!Object.prototype.isEmptyObject){ | |
Object.prototype.isEmptyObject = function(value){ | |
for (var key in value) { | |
if (hasOwnProperty.call(value, key)) { | |
return false; | |
} | |
} | |
return true; | |
} |
if(!String.prototype.trim){ //ES5 IE9+ | |
//regexp | |
String.prototype.trim=function(){ | |
return this.replace(/(^\s+)|(\s+$)/g, '') | |
} | |
} |
//Fisher-Yates 舒服了 | |
Array.prototype.shuffle=function(){ | |
let results = [], temp, acc = this.length >>> 0 | |
while(acc > 0){ | |
temp = (Math.random() * acc--)>>>0 | |
[this[acc],this[temp]] = [this[temp], this[acc]] | |
} | |
return this | |
} |
// demo | |
let privateDataStore = { | |
set(val){ | |
let key = Symbol(Math.random().toString(32).substr(2)) | |
this[key]=val | |
return key | |
} | |
get(key){ | |
return this[key] | |
} |
let fetch = ()=>{ | |
return new Promise((resolve, reject)=>{ | |
api.call('fetch_data', (err, data)=>{ | |
if(err) return reject(err) | |
resolve(data) | |
}) | |
}) | |
} | |
fetch('data/url/to') |
function* fib(){ | |
let a=1, b=1 | |
yield a | |
yield b | |
while(true){ | |
let next = a+b | |
a=b | |
b=next | |
yield next | |
} |