Friday, July 20, 2007

call() and apply() of Function methods

These methods allow you to invoke a function as if it were a method of some other object.

<script type="text/javascript" language="JavaScript">

function doc(str){
document.writeln(str);
document.writeln("<br />");
}

var str = 'test';
doc.call(String, str);

// This is similar to the following lines of code:
String.m = doc;
String.m(str);
delete String.m;

</script>

No comments :