首页 > 专栏 > 前端 > 文章详情
学习 js 之 apply() 和 call() 方法 发布于:2021-03-07 15:13:40   原创发表   查看:5  讨论:0
call():将某个实例的数据 调用 某个对象或函数的方法,一般原方法默认带返回值,可多个参数。bNA易塔云建站-模板下载,web开发资源,技术博客
call():我查到的中文意思是传呼,翻译成调用,比较让我容易记忆。bNA易塔云建站-模板下载,web开发资源,技术博客
apply():将某个对象或函数方法 应用到 某些实例元素,only 数组。就两个参数。bNA易塔云建站-模板下载,web开发资源,技术博客
apply():明显是一个应用,是强制型的,所以数组更适合它,也是方便记忆而已。
var person = {bNA易塔云建站-模板下载,web开发资源,技术博客
    fullName : function(){bNA易塔云建站-模板下载,web开发资源,技术博客
        return this.firstName + " " + " " + this.lastName;bNA易塔云建站-模板下载,web开发资源,技术博客
    }bNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
var person1 = {bNA易塔云建站-模板下载,web开发资源,技术博客
    firstName : "Firssss",bNA易塔云建站-模板下载,web开发资源,技术博客
    lastName : "Lastttttt"bNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
var person2 = {bNA易塔云建站-模板下载,web开发资源,技术博客
    firstName : "Firssss2222",bNA易塔云建站-模板下载,web开发资源,技术博客
    lastName : "Lastttttt2222"bNA易塔云建站-模板下载,web开发资源,技术博客
bNA易塔云建站-模板下载,web开发资源,技术博客
//将 person1的实例数据 调用 person.fullName()方法,该方法默认带一个返回值bNA易塔云建站-模板下载,web开发资源,技术博客
//返回值是一条语句字符串。bNA易塔云建站-模板下载,web开发资源,技术博客
var sentence = person.fullName.call(person1);bNA易塔云建站-模板下载,web开发资源,技术博客
alert(sentence)bNA易塔云建站-模板下载,web开发资源,技术博客
var sentence2 = person.fullName.call(person2);bNA易塔云建站-模板下载,web开发资源,技术博客
alert(sentence2)   
call():可以接受参数时。
var person = {bNA易塔云建站-模板下载,web开发资源,技术博客
    fullName : function(city,country){bNA易塔云建站-模板下载,web开发资源,技术博客
        // var city,country;bNA易塔云建站-模板下载,web开发资源,技术博客
        return this.firstName + " " + this.lastName + " " + city + " " + country;bNA易塔云建站-模板下载,web开发资源,技术博客
    }bNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
var person1 = {bNA易塔云建站-模板下载,web开发资源,技术博客
    firstName: "Yamaha",bNA易塔云建站-模板下载,web开发资源,技术博客
    lastName:"Yadi"bNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
var result = person.fullName.call(person1,"Seattle","Japanese")bNA易塔云建站-模板下载,web开发资源,技术博客
alert(result)
apply():关于上面的使用不做介绍,一样。
var person = {bNA易塔云建站-模板下载,web开发资源,技术博客
    fullName : function(city,country){bNA易塔云建站-模板下载,web开发资源,技术博客
        return this.firstName + " " + this.lastName + " " + city + " " + country;bNA易塔云建站-模板下载,web开发资源,技术博客
    }bNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
var person1 = {bNA易塔云建站-模板下载,web开发资源,技术博客
    firstName : "Firssss",bNA易塔云建站-模板下载,web开发资源,技术博客
    lastName : "Lastttttt"bNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
// var result = person.fullName.apply(person1,['Beijing','shijianzhuang'])bNA易塔云建站-模板下载,web开发资源,技术博客
var result = person.fullName.call(person1,'Beijing','shijianzhuang')//直接传数组返回 undefinedbNA易塔云建站-模板下载,web开发资源,技术博客
alert(result)
call中对指针的解读:
/*测试this指针的问题*/bNA易塔云建站-模板下载,web开发资源,技术博客
function add(param1,param2){bNA易塔云建站-模板下载,web开发资源,技术博客
    console.log(this);bNA易塔云建站-模板下载,web开发资源,技术博客
    return (this.a + this.b) + "\<br\>" + param1 + param2;bNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
function sub(param1,param2){bNA易塔云建站-模板下载,web开发资源,技术博客
    console.log(this);bNA易塔云建站-模板下载,web开发资源,技术博客
    return (this.a - this.b) + "pppppp" + param1 + param2;bNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
var num1 = {bNA易塔云建站-模板下载,web开发资源,技术博客
    a:1,bNA易塔云建站-模板下载,web开发资源,技术博客
    b:2bNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
var result = add.call(num1,"传过去参数,","不知何用?");bNA易塔云建站-模板下载,web开发资源,技术博客
//add中的this指向了该num1对象;{a: 1, b: 2}bNA易塔云建站-模板下载,web开发资源,技术博客
var result = add.call(add,"传过去参数,","不知何用?");bNA易塔云建站-模板下载,web开发资源,技术博客
//此时的add函数中打印出来的this,指向函数自己;f 函数书写体。bNA易塔云建站-模板下载,web开发资源,技术博客
var result = add.call(sub,1,2); bNA易塔云建站-模板下载,web开发资源,技术博客
//解释:将sub函数作为参数 调用 add的方法,该方法返回一个字符串bNA易塔云建站-模板下载,web开发资源,技术博客
//add函数中的this指向着 sub 函数,打印出 sub 函数体。bNA易塔云建站-模板下载,web开发资源,技术博客
var result = sub.call(add,1,2);bNA易塔云建站-模板下载,web开发资源,技术博客
//解释:sub函数的this指针 → add 函数,打印出 add 函数体。bNA易塔云建站-模板下载,web开发资源,技术博客
console.log(result);
继承自其它对象的属性,加入到自己内部:
/*继承来自于其他对象的属性*/bNA易塔云建站-模板下载,web开发资源,技术博客
function People(name,age){bNA易塔云建站-模板下载,web开发资源,技术博客
    this.name = name;bNA易塔云建站-模板下载,web开发资源,技术博客
    this.age = age;bNA易塔云建站-模板下载,web开发资源,技术博客
    console.log(this);bNA易塔云建站-模板下载,web开发资源,技术博客
    //此时的People()构造函数,已经被实例化;bNA易塔云建站-模板下载,web开发资源,技术博客
    //数据来源是call()括号里面的数据参数bNA易塔云建站-模板下载,web开发资源,技术博客
    //而数据参数在Student构造器并没有找到bNA易塔云建站-模板下载,web开发资源,技术博客
    //后面有实例化该语句,也就等于student实例延长了自己的作用域 至 People 构造器bNA易塔云建站-模板下载,web开发资源,技术博客
    //student 既拥有People构造器的全部属性,也拥有自己独有属性 gradebNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
function Student(name,age,grade){bNA易塔云建站-模板下载,web开发资源,技术博客
    People.call(this,name,age);bNA易塔云建站-模板下载,web开发资源,技术博客
    this.grade = grade;bNA易塔云建站-模板下载,web开发资源,技术博客
}bNA易塔云建站-模板下载,web开发资源,技术博客
var student = new Student("小明",21,"大三");bNA易塔云建站-模板下载,web开发资源,技术博客
console.log(student.name + student.age + student.grade);
数组,两个数组的操作,这时候apply用上了。
var arr1 = [1,2,3];bNA易塔云建站-模板下载,web开发资源,技术博客
var arr2 = [4,5,6];bNA易塔云建站-模板下载,web开发资源,技术博客
Array.prototype.push.apply(arr1,arr2);bNA易塔云建站-模板下载,web开发资源,技术博客
//第一个是要应用于的对象,目标对象。第二个是数据来源bNA易塔云建站-模板下载,web开发资源,技术博客
console.log(arr1);//[1,2,3,4,5,6]bNA易塔云建站-模板下载,web开发资源,技术博客
console.log(arr2);//[4,5,6]
var arr1 = [1,2,3];bNA易塔云建站-模板下载,web开发资源,技术博客
var arr2 = [1,5,6];bNA易塔云建站-模板下载,web开发资源,技术博客
Array.prototype.pop.apply(arr1);//删除最后一个数组,返回新数组。[1,2]bNA易塔云建站-模板下载,web开发资源,技术博客
Array.prototype.push.apply(arr1,arr2);//[1,2,1,5,6]bNA易塔云建站-模板下载,web开发资源,技术博客
console.log(arr1);//[1,2,1,5,6]

评论

  • 匿名