This example demonstrates how to create custom methods.

Basically all we've done is told the Table() Constructor Function that any object extended from it has access to the methods by adding the following syntax to the body of the Table() function: "this.methodName = functionName".

function Table(border,color) {
this.tableOpen = "<table border=" + border + ">"
this.trOpen = "<tr>"
this.tdOpen = "<td bgcolor=" + color + ">"
this.text = "<b>This border's size is " + border + " and the background is " + color + ".</b>"
this.tdClose = "</td>"
this.trClose = "</tr>"
this.tableClose = "</table>"

this.showTable1 = showTable1 //this.methodName = functionName, can be the same name
Then we took all the document.write() statements from the previous example and put them in a function body referencing the name given to it in the Table() function.
function showTable1() {
document.write("Here is some Text")
Now if you so desired, you could write 1,000 tables to the page simply by invoking the method on your new object 1,000 times. Sure beats coding each document.write() statement 1,000 times!
TableOne = new Table("10","orange") //extend object

TableOne.showTable1() // invoke method on object

View the Source

Home | Contact