Ein2012
Ein2012

Reputation: 1103

Is it the correct way to implement the javascript string length property?

I want to implement the JavaScript string inbuilt functions but i'm little confused with length property

Below is my implementation

function MyString(str){

    this.str=str;

    //properties
    this.length=0;

        if(typeof this.str[0]=='undefined')
            this.length=0;          

        else
            for (var i = 0; typeof this.str[i]!='undefined' ; i++) 
             this.length++;

        //tostring method
    this.toString=function(){
        if(typeof this.str == 'string')
            console.log(this.str);
        else
            console.log("cannot be converted to String") ;  

    }
}

Is this is the correct way to implement length property.Because it looks horrible for me !!.Or if i want to calculate length only when user calls 'str.length' and still length should remain as a property ,how do i do that

An alternative i've done is like that but will be a function

this.length=function(this.str){
//length logic here
}

How can i implement it in a better way but still length appears to be a property

Upvotes: 0

Views: 897

Answers (2)

Bergi
Bergi

Reputation: 664579

A string's length is not calculated in JavaScript. It is a constant that belongs to each string value. It can be queried using the .length property, which accesses the internally stored size of the respective string.

Just do

function MyString(str) {
    this.str = str;
    this.length = str.length;
}
MyString.prototype.toString = function() {
    return this.str;
};

If you want to follow the spec more closely, use

    Object.defineProperty(this, "length", {value: str.length});

Upvotes: 1

Naeem Shaikh
Naeem Shaikh

Reputation: 15715

I guess you are confused about( i assume you are trying to create your own string object):

  1. is it a better way of defining a property?

  2. is there any alternative way? and

  3. shall i put the logic for calculating the length inside a function and still use it as a property

If you have a property like string.length to build on your own, and you want to have some manipulations on it before it is initialized then you are doing it right.

A property will just be defined inside the constructor function, with all its initialization, like the way you have done, unless you you want to make it a function for length().

If you want to wrap your logic for length inside a function, that would actually become a method(and not remain a property anymore), i don't have any alternative way to override that

But if you want the logic: this one

        if(typeof this.str[0]=='undefined')
            this.length=0;          

        else
            for (var i = 0; typeof this.str[i]!='undefined' ; i++) 
             this.length++;

to be called whenever the length function(property what you want to make) is called, then you must create a method for it.

So Property length in this case wont be possible, unless you stick to your approach

Upvotes: 1

Related Questions