1 | 2 | 3 | 4 | 5

6 - 10   [21]

Parent Prototype Revisited

This post does not relate to any other topics

Further to my post regarding referencing the current object as an instance of its prototype (aka superclass) via super simulation, I have realised by maintaining the correct constructor property value in the object's prototype there is no need to pass the object constructor to the parent prototype method of Object.


Object.prototype.parent = function() {
  var p = new Object();
  for (x in this.constructor.prototype) {
    if (x != "parent") {
      p[x.toString()] = this[x.toString()];          
    }
  }   
  return p;  
}

function BoxContainer(param) {
  Container.call(this, param); 
  var parent = this.parent();

  [..snip..]
}

BoxContainer.prototype = new Container;
BoxContainer.prototype.constructor = BoxContainer;

Where the last line is critical, since the value of constructor is initially overriden with the assignment of the BoxContainer prototype to reference Container.

Posted on Oct 13, 2004 at 03:30:21. [Comments for Parent Prototype Revisited- 0]

Firefox Status: stopped

Also relates to Firefox and Co

Sometimes when visiting a site in Firefox I have found the page suddenly stops loading, is replaced by a blank screen, and, the status bar displays the text stopped. More often than not this is supplemented by an error thrown in Javascript for an undeclared variable. Well, as frustrating as this is on other sites, the same problem started occurring in the latest build I am working in. After a little code debugging, the error comes down to the use of the document.write method during page loading. I was using this method to declare some inline styles dynamically only if scripts are enabled - following the same prinicples outlined in the article I wrote on Accessible DHTML.

The fix seems to be to create the inline styles with the DOM regardless of the document being served as application/xhtml+xml mime type (in this case it was not). Since the head element is already in place when the external script is called, the inline styles can safely be appended:


if (document.getElementById && document.createElementNS) {
  var elem=document.createElementNS("http://www.w3.org/1999/xhtml","style");
  elem.setAttribute("type","text/css");
  var text=document.createTextNode(strCSS);
  elem.appendChild(text);
  document.getElementsByTagName("head")[0].appendChild(elem);
} else {
  strCSS = '<style type="text/css">' + strCSS + '</style>';
  document.write(strCSS);
}

Posted on Oct 10, 2004 at 15:40:22. [Comments for Firefox Status: stopped- 0]

Call Prototype for IE Legacy

Also relates to Exploder

One cautionary note I discovered when testing access to overriden methods is that IE5.01 does not recognize the call method. I had only recently started using this to inherit public access properties and methods from the constructor's prototype, mainly for asthetic reasons (and one less line of code). Well the IE fix, as performed previously with prototyping Array objects, is a new method for the Function object's prototype as follows:


if (typeof Function.prototype.call == "undefined") {
  Function.prototype.call = function(obj, param) {
    obj.base = this;
    obj.base(param);  
  }  
}

This simply acts as a wrapper for the traditional method of prototype inheritance, by calling the constructor function of the prototype, and fortunately is compatible with IE5.01.

Posted on Oct 08, 2004 at 04:47:21. [Comments for Call Prototype for IE Legacy- 0]

Super Simulation in Javascript OOP

This post does not relate to any other topics

Writing some Javascript the other day I found myself wanting to override a method of an object's prototypical object while still having access to the inherited method. That is, in class based terminology, I wanted to be able to invoke the overriden method of the superclass. In Java this is achieved with the keyword super which provides a reference to the current object as an instance of its superclass. However, as a prototype-based language, Javascript does not provide such a reference. Kevin Lindsey demonstrates the recursive dilemma and suggests an idiom for simulating constructor chaining by utilising the ECMA compliant call method. While this could serve my purpose, I decided to look for a way to create a single reference to access overriden member variables and methods (just like super).

Here is one possible solution:


Object.prototype.parent = function(type) {
  var p = new Object();
  for (x in type.prototype) {
    if (x != "parent") {
      p[x.toString()] = this[x.toString()];          
    }
  }   
  return p;  
}

function BoxContainer(param) {
    
  Container.call(this, param);
    
  var parent = this.parent(BoxContainer);
  
  this.service = function() {
    if (this.service.arguments.length == 0) {    
      return parent.service();    
    }
    else {
      // .. do some stuff ..
      return stuff;  
    }
  }  
}
BoxContainer.prototype = new Container;

The prototype object, Container, is that used by Douglas Crockford in his article Private Members in Javascript.

A new method, parent, is added to the prototype property of the Object object. This method allows an temporary object to be filled with the public access variables and methods of a prototype before they are overriden. So, the constructor function, BoxContainer, invokes this method passing its prototypical object. The method returns a new object reference filled with all the properties and methods of the prototypical object, and this is assigned to a private variable, parent. Then a closure allows the inner function service to maintain access to parent after the constructor function BoxContainer has returned. (NB while super may have been an elegant choice for the identifier, it is a reserved word, so I choose to use parent - not to be confused with DOM!)

Posted on Oct 08, 2004 at 04:44:50. [Comments for Super Simulation in Javascript OOP- 6]

Web Based Applications

Also relates to PHP and Accessibility

Over on WASP Chris Kaminski talks about the future of the web as a platform for developing and deploying applications. In the wake of Javascript's XmlHttpRequest this has become a very popular topic for discussion. The ability to call data from the server via client-side scripting without the need to reload the whole page is an exciting prospect and already some excellent utilities are emerging. Bitflux led the way with Live Search which is under further development over at Colly Logic. Meanwhile, Harry Fuecks, in follow up to his Crouching Javascript, Hidden PHP series, has established the SourceForge project ScriptServer - a complete set of tools for connecting PHP and Javascript together. Although proprietary in detail, Jonathan Zufi applies the M$ implementation of XmlHttpRequest to form validation with VBScript and ASP. On a much grander scale, Gmail and A9 both offer fine examples of web based applications powered by considerable Javascript.

Personally, I am excited by the new opportunities provided by a web based platform and it is nice to see client side scripting appearing in the spotlight again. The communication between client-side and server-side scripts is providing plenty of potential for increased usability and enhanced UI. However, there is one critical concern with any application that puts major emphasis on client side scripting - accessibility. Fortunately A9 still functions with scripts disabled, however GMail currently displays that gratuitous javascript is required message! Done right the enhancements provided by XmlHttpRequest and other DHTML tools can improve the usability of an application without reducing its accessibility. But a dependance on scripting could see a drastic demise to former times and a blank screen served upto 20% of the web!

Posted on Oct 03, 2004 at 02:48:18. [Comments for Web Based Applications- 0]

Breadcrumbs Trail

[ Home ] -> TW Blog -> DOM Scripting
Site Map

The Severn Solutions website achieves the following standards:

[ XHTML 1.0 ] [ CSS 2 ] [ WAI AA ] [ Bobby AA ]

Page compiled in 0.000 seconds