Web design templates - 1118 Part IV . JavaScript Core Language Reference
Sunday, September 30th, 20071118 Part IV . JavaScript Core Language Reference Better ways exist to intercept and preprocess user input, but the watch()func tion can be a helpful debugging tool when you want to monitor the hidden workings of scripts. Defining object property getters and setters A future version of the ECMA-262 language specification will likely include a pair of facilities called getter and setter. Until such time as the formal syntax is finalized, you can begin to experiment with this technique in NN6 using temporary syntax that adheres to the likely format (but intentionally uses different keywords until the standard is adopted). When the standard is adopted, a subsequent version of NN will include the standard keywords. I introduced the idea of creating a getter and setter for an object briefly in Chapter 14, where the NN6 syntax style extended properties of some W3C DOM objects to include some of the Microsoft-specific (and very convenient) DOM syn tax. Most notably, you can define a getter for any container to return an array of nested elements just like the IE-only document.all collection. The purpose of a getter is to assign a new property to the prototype of an object and to define how the value returned by the property should be evaluated. A setter does the same, but it also defines how a new value assigned to the property should apply the value to the object. Both definitions are written in the form of anonymous functions, such that reading or writing an object s property value can include sophisticated processing for either operation. Getters and setters are assigned to the prototypeproperty of an object, thus enabling you to customize native and DOM objects. The NN6 syntax fashions get ters, setters, and methods of an object s prototype with the following syntax: object.prototype.__defineGetter__( propName , function) object.prototype.__defineSetter__( propName , function) Note that the underscores before and after the method names are actually pairs of underscore characters (that is, _, _, defineGetter, _, _). This double under score was chosen as a syntax that the ECMA standard will not use, so it will not conflict with the eventual syntax for this facility. The first parameter of the method is the name of the property for which the get ter or setter is defined. This can be an existing property name that you want to override. The second parameter can be a function reference; but more likely it will be an anonymous function defined in place. By using an anonymous function, you can take advantage of the context of the object for which the property is defined. For each property, define both a getter and setter even if the property is meant to be read-only or write-only. To see how this mechanism works, let s use the getter and setter shown in Chapter 14 to add an innerTextproperty to HTML elements in NN6. This property is read/write, so functions are defined for both the getter and setter. The getter defi nition is as follows: HTMLElement.prototype.__defineGetter__( innerText , function () { var rng = document.createRange() rng.selectNode(this) return rng.toString() })
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.