CODECUBE VENTURES

Making Javascript more like VBScript

For the lazy of typing out there, now you can have the easy to use functions that VBScript has in your favorite client side language!

For those of you that didn't know, the javascript language gives you the ability to add methods to existing objects. This means that if you want to add a new method to the String object, you can.

This is exactly what I set out to do because I specifically like the simple string functions left, and right. If you don't know VBScript, these functions will give you the left or right (number of characters that you specify)

Example:

var = left("hello you",4)

The above example will return the letters "hell", the right function works the same way, but starts from the end of the string. I wanted the simplicity of using these in javascript, so I used the prototype property to add new methods to the string object.

Example:

`function sWrite() {

document.write(this.toString());

}

String.prototype.write = sWrite;`

The above example adds a "write" method to the string object, now all you need to do to get text on the screen from javascript is this:

` var text = "This is text";

text.write();`

I've created a file that can be downloaded here that adds the following methods to the string object.

  • write() : Writes a line

  • writeLine(): Writes a line and inserts an html break at the end

  • left(num): returns the num amount of characters at the beginning of the string

  • right(num): returns the num amount of characters at the end of the string

  • replace(what, withwhat): returns a string with what replaced with withwhat (this can be any combination of characters in the string

  • toHtml(): returns text that is "HTML encoded" (not really, I just replaced line breaks with html breaks, spaces with non breaking spaces, and other thing of this nature)

If you have any improvements over this code, please Email Me

Latest post: Digging Up the First Version of CodeCube

See more in the archives