• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 31. Programmable Elements > Example: Reorganizing a Script

Example: Reorganizing a Script

As you know by now, any JavaScript statements outside a function but inside a script execute automatically. As scripts get larger and larger (and I do write some fairly large ones), I have a habit of placing certain statements immediately after the function to which they relate. For instance, take this excerpt from Listing 1.10:

Object.prototype.instances = [""]

function addInstance() {
  if (!this.instanceIndex) {
   this.instanceIndex = this.instances.length
   this.instances[this.instanceIndex] = this
   }
  }
Object.prototype.addInstance = addInstance

function getInstanceName() {
  return "Object.instances["+this.instanceIndex+"]"
  }
Object.prototype.getInstanceName = getInstanceName

function testObj() {
  this.value = 0
  this.addInstance()
  }

function test(x) {
  if (x.value < 100) {
   x.value += 10
   HTML += x.value + "<br />"
   setTimeout("test(" + x.getInstanceName() + ")")
   } else {
   HTML += "Pass"
   document.write(HTML)
   }
  }

HTML = ""
k = new testObj()
test(k)


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint