aplay和call
每个函数都有两个非继承而来的方法apply()和call(),这两个方法的用途都是用来调用函数(在特定的作用域中),实际上等于设置函数体内的this对象的值;
区别
function Monkey(newId,newName){ this.id = newId; this.name = newName; } function Snake(newId,newName){ this.id = newId; this.name = newName; } function eat(str,str1){ console.log(this.name + "吃" + str + "和" + str1); } let m = new Monkey(1,"泰山"); let s = new Snake(2,"小可爱"); //参数1为实力对象,后续参数为eat的参数 eat.call(m,"香蕉","牛奶"); eat.call(s,"老鼠","人"); eat.apply(m,["香蕉","牛奶"]); eat.apply(s,["老鼠","人"]); apply与call的区别 function fun(){ let m = new Monkey(1,"泰山"); eat.apply(m,arguments); } fun("香蕉","牛奶");
apply()和call() 真正的用途:
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/230629.html原文链接:https://javaforall.net
