[关闭]
@panhonhang 2019-07-23T16:05:00.000000Z 字数 2643 阅读 463

js基础 假期第一周

学习


柯里化函数实现:

let curryAdd = (...args)=>{
    let add = function(...addAarry) {
        let list = [...addAarry];
        return list.reduce((pre,next)=> pre+next)
    }
    let curry = function(fn,...ThisAgrs){
        let list = [...ThisAgrs];
        return function(...ThisAgrs){
            if (ThisAgrs.length==0){
                return add.apply(this,list);
            } else {
                that = [...ThisAgrs,...list];
                return curry(fn,...that);
            }
        }
    }
    return curry(add,...args);
}

let result = curryAdd(2,2)(2)();

重点:apply方法的使用,reduce方法,闭包的使用,rest参数

call与apply的用法,bar.call(a,'aa')但是当第一个参数为null的时候,指向全局对象。

模拟实现call 方法

Function.prototype.call1 = function(con){
con.fn = this;
con.fn();
delete con.fn;
}

let bar = ()=>{
    console.log(this.value);
}

let a = {
    value:"aaa"
}
bar.call1(a);
//undefind

原因:
箭头函数有几个使用注意点。

(1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。

(2)不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。

(3)不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用 rest 参数代替。

(4)不可以使用yield命令,因此箭头函数不能用作 Generator 函数。

没有prototype属性,不能使用yeild关键字,不能做生成器

因为箭头函数的this绑定在自己定义的时候的this不能被修改的。无论是call apply 还是 bind来说都不能改变箭头函数的this指向。

正确写法:

Function.prototype.call1 = function(con){
con.fn = this;
con.fn();
delete con.fn;
}
let bar = function(){
    console.log(this.value);
}
let a = {
    value:"aaa"
}

bar.call1(a);

实现思路:bar.call(a)相当于是a.bar()

最终修改:

Function.prototype.call1 = function(ag){
console.log(ag);
let con = ag || window;
con.fn = this;
let list = [...arguments];
list.shift();
con.fn(...list);
delete con.fn;

}

let bar = function(name,age,lalala){
    console.log(this.value);
    console.log(name);
    console.log(age);
    console.log(lalala);
}

var value = "aasasdasdsa";

let a = {
    value:"aaa"
}

bar.call1({},'pan',22,'hdsfgsadtgjhf');

let 声明的变量在全局作用域但是不是windows对象

var b = 2;
console.log(this.b);
//打印为2

var b = 2;
console.log(this.b);
//打印undefind

var i = 1;
let i = i;
//报错重复声明i

settimeout与setinterval用法
setTimeout(alert, 1000, 'Hello world!');参数问题

bind模拟实现:

Function.prototype.apply1 = function(con){
    con.fn = this;
    let list = [...arguments];
    list.shift();
    let result = con.fn(...list[0]);
    delete con.fn;
    return result;
}


 Function.prototype.bind1 = function bind1(con){
    self.fn  = this;
    let list = [...arguments];
    list.shift();
    return function(){
        return self.fn.apply1(con,list);
    }
}


let a = function(name,age){
    console.log(this.value);
    console.log(name,age);
}

let b = {
    value: 1
}

let c = {
    value: 3
}

a.bind1(b,'aa','aa').bind(c)();

重点:返回为一个函数,还可以传入参数。
解释为什么可以链式调用:因为返回的是一个函数,而每一个函数都有bind方法;
解释为什么两次bind但是this依然指向第一个对象:因为第一次绑定的时候已经this指向了第一个对象了,然后返回一个函数,第二次绑定的时候,返回的函数的this指向第二个对象。但是不是第一次的函数。

reduce实现:

Array.prototype.reduce1 = function(fn){
    let arr = this[0];
    if(this.length == 0){
        throw new TypeError("Reduce of empty array with no initial value");
    }
    let result = this[0];
    for(let i=1; i<this.length; i++){
        result = fn(result,this[i],i,this);
    }
    return result;
}

let task = [1,23];
let n = (a,b,c,d)=> {
    console.log(a,b,c,d)
    return a*b;
};

console.log('my',task.reduce1(n));
console.log('true',task.reduce(n));
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注