String constructor - Programmers Heaven

Howdy, Stranger!

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


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.