String constructor - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


String constructor

psutinrerkpsutinrerk Posts: 1Member
Hello all,

I am reading a object-oriented javascript by Stoyan Stefanov and try to do one of the exercise as follow

Imagine the String() constructor didn't exist. Create a constructor function MyString() that acts like String() as closely as possible. You're not allowed to use any built-in string methods or properties, and remember that String() doesn't exist. You can use this code to test your constructor:
>>> var s = new MyString('hello');
>>> s.length;
>>> s[0];
>>> s.toString();

the following is my code
just wondering if anyone have the better solution or any suggestion to improve my code.

any suggestion would be really appreciated

Thank you

function MyString(text){
this.lenght = 0;
for(var c in text){
this[this.lenght] = text[c];
this.toString = function(){
var tmp = [];
for(var i=0; i<this.lenght; i++){
tmp[i] = this[i];
return tmp.join("");
return this;

Sign In or Register to comment.