March 21, 2000 - Calls By Reference | WebReference

March 21, 2000 - Calls By Reference

Yehuda Shiran March 21, 2000
Calls By Reference
Tips: March 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Version 4.0 of DOCJSLIB introduces many functions that expect the DHTML element to be passed by reference. What this means is that the object itself should pass to the appropriate function. This is in contrary to the older functions in DOCJSLIB that expect object ID to be passed as a string. You can check if the element is passed by reference or by string, by simply printing it to an alert box. The object passed by reference will be printed as an object, while the one passed by string will be printed as a string. Objects are created in JavaScript by the new operator.

Let's take an example that shows the differences between call-by-reference and call-by-string. The function setVisibility() sets the visibility of a passed-by-string element:

function docjslib_setVisibility(id, flag) {
  if (NS4) {
    var str = (flag) ? 'show' : 'hide';
    eval("document." + id).visibility = str;
  }
  else {
    var str = (flag) ? 'visible' : 'hidden';
    eval("document.all." + id).style.visibility = str;
  }
}

The following function sets the visibility of elements that are passed by reference (objects):

function docjslib_setElementVisibility(id, flag) {
  if (NS4) {
    var str = (flag) ? 'show' : 'hide';
    eval(id).visibility = str;
  }
  else {
    var str = (flag) ? 'visible' : 'hidden';
    eval(id).style.visibility = str;
  }
}

Learn more about DOCJSLIB 4.0 in Column 33, DOCJSLIB Version 4.0: Scrollers, Watermarks, and Games

.