JavaScript--appyの使用
5806 ワード
Js apply
:
1.apply call
2. apply, call
3.apply ( apply)
apply call , .
apply: , .
Function.apply(obj,args)
obj: Function this
args: , Function(args-->arguments)
call: apply , .
Function.call(obj,[param1[,param2[,…[,paramN]]]])
obj: Function this
params:
1.apply :
<script type="text/javascript">
/* */
function Person(name,age) {
this.name=name; this.age=age;
}
/* */
functionStudent(name,age,grade) {
Person.apply(this,arguments); this.grade=grade;
}
//
var student=new Student("qian",21," ");
//
alert("name:"+student.name+"
"+"age:"+student.age+"
"+"grade:"+student.grade);
// name:qian age:21 grade:
// name age , , apply .
</script>
: Person.apply(this,arguments);
this: student
arguments: , [“qian”,”21”,” ”];
: student Person , Person this.name , student
2.call
Studen apply :
Person.call(this,name,age);
ok
3. apply, call
, , apply arguments, , Person ( Person Student ) apply , Person (age,name), Student (name,age,grade), call , (Person.call(this,age,name,grade));
4.apply
, apply , (this), ,
Person , , ,
apply , ([param1,param2,param3] param1,param2,param3) , , , apply , :
a)Math.max
Math.max Math.max([param1,param2])
Math.max(param1,param2,param3…), apply var max=Math.max.apply(null,array),
(apply )
null, , , ,. null
b)Math.min
max var min=Math.min.apply(null,array);
c)Array.prototype.push
push push , push(param1,param,…paramN) apply , :
vararr1=new Array("1","2","3");
vararr2=new Array("4","5","6");
Array.prototype.push.apply(arr1,arr2);
,arr1 push , apply .
, apply Math.min :
n , ([param1[,param2[,…[,paramN]]]]), apply !
5. :
apply , , , , , , , , …
, , ( ), , , , , !