JS has never been very good! Previously viewed by othersCodeI have no idea what call or apply means! I learned today! More information comes from the network! Thank you!
Question:
Function. prototpe. Apply (instance, argS) // ARGs Array
Function. prototpe. Call (instance, a1, a2) // a1, a2 single parameter
/**
* People
*/
Function people (){
This. Name = 'jinho ';
This. showname = function (){
If (arguments) {alert (this. Name + "\ r \ NARG: =:" + arguments. Length); return ;}
Alert (this. Name );
};
}
/*
* Student
*/
Function student (){
This. Name = 'student ';
}
VaR P = new people; // create an object
VaR S = new student; // create an object
P. showname. Call (s); // output "'student '", indicating that the current this of the showname function has changed to P. What's amazing! The S object does not have the showname () method! He can still execute it! It is because the call function regards S as this!
P. showname. Apply (s); // output "'student '"
// The Difference Between the call function and the apply function is that the call syntax is function. Call (OBJ, param1, param2 ......); The syntax of applay is function. Call (OBJ, []/* Params [] parameter array */);
this article from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/jinho/archive/2010/03/11/5371933.aspx