Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Upgrading Native Objects for Older Browsers"

Status
Not open for further replies.

enderhegemon

Technical User
Jan 29, 2006
7
CA
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!
 
From what the book says, and the code shown, the two functions "_Apply_" and "_Call_" are simply providing access to functions/methods that newer browsers support, but for older browsers.

This is what these lines do:

Code:
if (!Function.prototype.apply) Function.prototype.apply = _Apply_;
if (!Function.prototype.call) Function.prototype.call = _Call_;

Basically, they say that if the Function object does not have methods called "apply" or "call" (i.e. if the browser is too old to support them, or happens not to support them), then they are given this support by means of the two functions shown.

This is no different, for example, than providing support for the Push and Pop methods of the Array object for browsers that do not support them.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top