Showing posts with label Tricks. Show all posts
Showing posts with label Tricks. Show all posts

Module in JavaScript

Example fo creating a module in Javascript

Class definition in Javascript

defining a class in Jscript

JavaScript StringBuilder

a simple StringBuilder class that pushes individual strings into an array and then uses the join method to produce the concatenated output string. The code is simple and straightforward that it should be self-explanatory. Now here's an example of how to use it:
// create a StringBuilder
var sb = new StringBuilder();

// append some text
sb.append("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, ");
sb.append("sed diem nonummy nibh euismod tincidunt ut lacreet dolore ");
sb.append("magna aliguam erat volutpat.");

// get the full string value
var s = sb.toString();

Counting the number of backslashes in a string

First method


var text = "hardware\\printers\\Epson";
var paths = text.split("\\");
alert(paths.length -1);



second method


var text = "hardware\\printers\\Epson";
var numBs = 0;
var index = 0;

while(index != -1) {
index = text.indexOf("\\", index);

if (index != -1) {
numBs++;
index++;
}
}

alert(numBs);