Skip to content

Instantly share code, notes, and snippets.

@ratpik
Last active March 5, 2017 12:17
Show Gist options
  • Save ratpik/388851be432cc4c11e26885f5f1f3297 to your computer and use it in GitHub Desktop.
Save ratpik/388851be432cc4c11e26885f5f1f3297 to your computer and use it in GitHub Desktop.
JS BinRepresentation of a custom String Object// source https://jsbin.com/jibuso
var MyString = function(source){
var arr = [];
for(var index in source){
arr.push(source[index]);
//Public property
this[index] = source[index];
}
//Public property
this['length'] = arr.length;
//Public getter
this.getStringArray = function() {
return arr;
}
};
MyString.prototype.toString = function(){
return this.getStringArray().join('');
};
MyString.prototype.charAt = function(i){
return this.getStringArray()[i];
};
MyString.prototype.concat = function(source2){
return this.toString() + source2;
};
MyString.prototype.slice = function(start, end){
var slice = '';
var arr = this.getStringArray();
for(var i=start; i < end; i++){
slice += arr[i];
}
return slice;
};
MyString.prototype.split = function(char){
var pre = '', post = '', split = '';
var arr = this.getStringArray();
var i = 0;
while(arr[i] != char){
pre += arr[i++];
}
i++;
while(arr[i] !== undefined){
post += arr[i++];
}
return [pre, split, post];
};
MyString.prototype.reverse = function(){
var arr = this.getStringArray();
var s = '';
for(var i = arr.length - 1; i >= 0; i--){
s += arr[i];
}
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment