Skip to content

Instantly share code, notes, and snippets.

@mactive
Last active March 6, 2017 02:41
Show Gist options
  • Save mactive/d104d8284a351c8858a9a0a2525ee838 to your computer and use it in GitHub Desktop.
Save mactive/d104d8284a351c8858a9a0a2525ee838 to your computer and use it in GitHub Desktop.
// 函数的绑定
let sendMsg = {
ele: document.querySelector('#element'),
change:function(){
this.ele.classList.toggle("active"); //改变状态
}
}
document.querySelector('button')
.addEventListener('click',
bind(sendMsg.change,sendMsg)
,false);
function bind(fn, context){
return function(){
fn.apply(context, arguments);
}
}
//检测函数类型
function getType(value){ //基本上可以返回所有的类型,不论你是自定义还是原生
return Object.prototype.toString.call(value).match(/\s{1}(\w+)/)[1];
}
let obj = new Object();
console.log(getType(obj)); //"Object"
function Car(name){
// this.name = name;
if(this instanceof Car){
console.log('has been newed');
this.name = name;
}else{
console.log('need newed');
return new Car(name);
}
}
Car.prototype.run = function(){
console.log(this.name + ' run');
}
var t1 = new Car('t1');
t1.run();
var t2 = Car('t2');
t2.run();
//这是JS高程上面的例子
function curry(fn){
var args = Array.prototype.slice.call(arguments,1);
return function(){
var innerArgs = Array.prototype.slice.call(arguments); //[3]
var final = args.concat(innerArgs); //[5,3]
return fn.apply(this, final);
}
}
function add(num1,num2){
return num1+num2;
}
var Cadd = curry(add,5);
console.log(Cadd(3)); //8
console.log(Cadd(5)); //10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment