Extended the javascript string operator

If you want to extend a javascript object:

// Define the function to do what you want
function first3char() {
    return this.substr(0,3); // 'this' will be the variable that's passed in
}

// Extend the javascript string operator
String.prototype.first3=first3char;

// Call your extension using the string 'abc123'
"abc123".first3();
Leave A Reply
All content licensed under the Creative Commons License