enderhegemon
Technical User
Hi,
I'm currently reading Professional JavaScript 2nd Edition (Wrox Publishing; ISBN: 1-861005-53-9) and there is a section on "Upgrading Native Objects for Older Browsers" which has completely confused me (pg. 89). If anyone can give me a clear explanation of the code provided, it would be greatly appreciated.
Here is the text before the code is given:
"JavaScript 1.3...introduced two very useful methods to the Function object, which are used frequently in this chapter: call() and apply() methods, which are both used to call the function.
These two methods were including (sic) in the ECMAScript v3 standard, and Microsoft included them in Internet Explorer 5.5...They are very important to the implementation of inheritance as presented in this chapter. A means of providing these methods to older version of JavaScript is presented below through the example of extending a class of objects via properties of the prototype of the Function constructor:"
-----------------------------------------------------------------
//Patch the apply method of the Function class of objects if needed
function _Apply_ (thisObj, argArray)
{
var str, i, len, retValue;
if (thisObj + "" == "null" || thisObj + "" == "undefined"
thisObj = window;
if (argArray + "" == "null" || argArray + "" == "undefined"
argArray = new Array ();
//make sure that the property getting the method is unique
var index = 0;
while (thisObj ["temp" + index] + "" != "undefined") index++;
thisObj["temp" + index] = this;
str = "thisObj.temp" + index + "(";
len = argArray.length;
for (i = 0; i < len; i++)
{
str += "argArray[" + i + "]";
if (i + 1 < len) str += ", ";
}
str += ");";
retValue = eval(str);
thisObj["temp" + index] = undefined;
return retValue;
}
if (!Function.prototype.apply) Function.prototype.apply = _Apply_;
//Patch the call method of the Function class of objects if needed
function _Call_(thisObj, arg1, arg2, argN)
{
return this.apply(thisObj, Array.apply(null, arguments).slice(1));
}
if (!Function.prototype.call) Function.prototype.call = _Call_;
//Patch the undefined value ofr compatability for older browsers
var undefined;
-----------------------------------------------------------------
Thank you!
I'm currently reading Professional JavaScript 2nd Edition (Wrox Publishing; ISBN: 1-861005-53-9) and there is a section on "Upgrading Native Objects for Older Browsers" which has completely confused me (pg. 89). If anyone can give me a clear explanation of the code provided, it would be greatly appreciated.
Here is the text before the code is given:
"JavaScript 1.3...introduced two very useful methods to the Function object, which are used frequently in this chapter: call() and apply() methods, which are both used to call the function.
These two methods were including (sic) in the ECMAScript v3 standard, and Microsoft included them in Internet Explorer 5.5...They are very important to the implementation of inheritance as presented in this chapter. A means of providing these methods to older version of JavaScript is presented below through the example of extending a class of objects via properties of the prototype of the Function constructor:"
-----------------------------------------------------------------
//Patch the apply method of the Function class of objects if needed
function _Apply_ (thisObj, argArray)
{
var str, i, len, retValue;
if (thisObj + "" == "null" || thisObj + "" == "undefined"
thisObj = window;
if (argArray + "" == "null" || argArray + "" == "undefined"
argArray = new Array ();
//make sure that the property getting the method is unique
var index = 0;
while (thisObj ["temp" + index] + "" != "undefined") index++;
thisObj["temp" + index] = this;
str = "thisObj.temp" + index + "(";
len = argArray.length;
for (i = 0; i < len; i++)
{
str += "argArray[" + i + "]";
if (i + 1 < len) str += ", ";
}
str += ");";
retValue = eval(str);
thisObj["temp" + index] = undefined;
return retValue;
}
if (!Function.prototype.apply) Function.prototype.apply = _Apply_;
//Patch the call method of the Function class of objects if needed
function _Call_(thisObj, arg1, arg2, argN)
{
return this.apply(thisObj, Array.apply(null, arguments).slice(1));
}
if (!Function.prototype.call) Function.prototype.call = _Call_;
//Patch the undefined value ofr compatability for older browsers
var undefined;
-----------------------------------------------------------------
Thank you!