Web hosting bandwidth - 190Part IIIDocument Objects Referenceobject.prototype.__defineSetter__( propName , function([param1[,…[,paramN]]]) { // statementsreturn
190Part IIIDocument Objects Referenceobject.prototype.__defineSetter__( propName , function([param1[,…[,paramN]]]) { // statementsreturn returnValue; }) The example in Listing 14-3 demonstrates how to add a read-only property to every HTMLelement object in the current document. The property, called childNodeDetail, returns anobject; the object has two properties, one for the number of element child nodes and one forthe number of text child nodes. Note that the thiskeyword in the function definition is a ref- erence to the object for which the property is calculated. And because the function runs eachtime a script statement reads the property, any scripted changes to the content after the pageloads are reflected in the returned property value. Listing 14-3: Adding a Read-Only Prototype Property to All HTMLElement Objects To access the property, use it like any other property of the object. For example: var BodyNodeDetail = document.body.childNodeDetail; The returned value in this example is an object, so you use regular JavaScript syntax toaccess one of the property values: var BodyElemNodesCount = document.body.childNodeDetail.elementNodes; Bidirectional event modelDespite the seemingly conflicting event models of NN4 (trickle down) and IE4 (bubble up), the W3C DOM event model (defined in Level 2) manages to employ both event propagationmodels. This gives the scripter the choice of where along an event s propagation path theevent gets processed. To prevent conflicts with existing event model terminology, the W3Cmodel invents many new terms for properties and methods for events. Some coding probablyrequires W3C DOM specific handling in a page aimed at multiple object models.