Generic mutable String Object
A mutable String Object
by KooiInc MeHere
HTML
<div id="result"></div>
CSS
body {font: 0.8em normal verdana,arial; color:#c0c0c0}
code {color:green; font-size:120%;}
b {color: #000}
.codestuff{
color: green;
padding-left:1em;
font: normal 1em courier new;
}
JavaScript
'use strict';
helpers(window); //initialize log function
var test = StringX('hi there')
,test1 = test.copySelf()
,test2 = test.copy()
;
//are instanceMethods derived from native String methods?
log('<h3>Were we able to apply ','String.getOwnPropertyNames? '.toCode(),
test.nativeCando ? 'Yep' : 'Nope','</h3>');
//instance methods
log('<b>Create StringX instance and ap/prepend some text to it</b>');
log('var test = StringX(\'hi there\');'.toCode(),' // test = \'',test,'\'');
test.append(' mister X');
log('<code>test.append(\' mister X\');</code> // test = \'',test,'\'');
test.prepend('Anyway, ');
log('<code>test.prepend(\'Anyway, \');</code> // test = \''+test+'\'');
//use native String methods
log('<br><b>Apply regular String methods</b>');
test.replace('mister X','mister Y');
log('<code>test.replace(\'mister X\',\'mister Y\');</code> // test = \''+test+'\'');
test.slice(8);
log('<code>test.slice(8);</code> // test = \''+test+'\'');
log('<code>StringX(\' oh pleaze trim me? \').trim();</code> // ['+
StringX(' oh pleaze trim me? ').trim()+']');
//copy (instance or value)
log('<br><b>Copy Object</b>');
log('<code>var test1 = test.copySelf(); test1 instanceof StringX;</code> '+
'// '+(test1 instanceof StringX));
log('<b>is test1 a real copy?</b>');
log('<code>test1 !== test;</code> '+
'// '+(test1 !== test));
test1.empty().append('I am test1, really!').prepend('Let me tell you: ');
log('<code>test1.empty().append(\'test1 replaced string\').'
,'prepend(\'Let me tell you: \');</code><br> // '
,'test1 = \''+test1+'\''
,'<br> // (and test is still \''+test+'\')'
,'<br> // notice the chaining, btw');
log('<br><b>Copy value (string)</b>');
log('<code>var test2 = test.copy(); test2 instanceof StringX;</code> '
,'// '+(test2 instanceof StringX));
//use regular expressions
log('<br><b>You can use Regular...