标签:
相同点:两个方法产生的作用是完全一样的
不同点:方法传递的参数不同
用途都是在特定的作用域中调用函数,实际上等于设置函数体内this对象的值。
1 function Person(name,age){ 2 this.name = name; 3 this.age age; 4 } 5 function Student(name,age,grade){ 6 Person.apply(this.arguments); 7 this.grade = grade; 8 } 9 var student = new Student("123‘,12,"三年级"); 10 alert("name:"+student.name+"age:"+student.age+"grade:"+student.grade); 11 //name:123 age:12 grade:三年级
1 var value = [1,2,3,4,5,6,7,8,9]; 2 3 var max = Math.max.apply(Math.value);
1 var arr1 = new Array("1","2","3"); 2 3 var arr2 = new Array("4","5","6"); 4 5 6 7 Array.prototype.push.apply(arr1,arr2);
1 var toArray = function(s){ 2 try{ 3 return Array.prototype.slice.call(s); 4 }catch(e){ 5 var arr = []; 6 for(var i = 0, len = s.length; i< len;i++) 7 arr[i] = s[i];(arr.push(s[i])) 8 return arr; 9 } 10 }
1 window.color = "red"; 2 var o = {color:"blue"}; 3 4 function sayColor(){ 5 alert(this.color); 6 } 7 8 sayColor();//red 9 sayColor.call(this);//red 10 sayColor.call(window);//red 11 sayColor.call(o);//blue
1 window.color = "red"; 2 var o = {color:"blue"}; 3 4 function sayColor(){ 5 alert(this.color); 6 } 7 8 sayColor();//red 9 sayColor.call(this);//red 10 sayColor.call(window);//red 11 sayColor.call(o);//blue
标签:
原文地址:http://www.cnblogs.com/solomonhit/p/4419440.html