An important feature of apply is the change of the current executing method's this object;
For example:
<input type= ' file ' id= ' test '/>
First we define a method to detect the suffix name of the file, at this point the This object is pointing to window, we look at the following two test examples:
function Checkfiletype () {
var _value=this.value;
var pattern=/\. [a-za-z]$/;
var result=pattern.exec (_value);
if (result!=null)
Console.log (result);
Else
Console.log (' not find ');
}
So now we're going to use apply:
$ ("#test"). Click (function () {
Checkfiletype ();
});
$ ("#test"). Click (function () {
Checkfiletype.apply (this);
});
At this point we will find that the first one is not find, and the second is the correct one to print out the name of the attachment suffix.
By this we have introduced the Apply two core usage, inheriting and changing the current execution method of the This object.
JavaScript apply--(2)