JavaScript prototypes

Results 1 to 2 of 2

Thread: JavaScript prototypes

  1. #1
    Join Date
    Dec 1969

    Default JavaScript prototypes

    Has anyone around these parts worked with JavaScript prototypes and creating your own JS objects?<BR><BR>I am trying to create my own JS String object so that I can add methods that handle things such as string trimming that the default JS object does not handle. I&#039;m having NO problem inheriting the existing JS String methods, but for some reason I can not retrieve the string length of the inherited object. It always returns 0. Here&#039;s a simple example of the code:<BR><BR>function CSMString(str)<BR>{<BR> this.value = str.toString();<BR>}<BR><BR> nstructor = CSMString;<BR>CSMString.prototype = new String();<BR>CSMString.prototype.toString = function() {return this.value; }<BR>CSMString.prototype.LTrim = LeftTrim;<BR><BR>function LeftTrim()<BR>{<BR> var whitespace = new String("
    ");<BR><BR> var s = new String(this.value);<BR> <BR> if (whitespace.indexOf(s.charAt(0)) != -1) {<BR><BR> var j=0, i = s.length;<BR><BR> while (j &#060; i && whitespace.indexOf(s.charAt(j)) != -1)<BR> j++;<BR><BR> s = s.substring(j, i);<BR> }<BR> return s;<BR>}<BR><BR>/*** Debugging function. Nothing more. Nothing less ***/<BR>function showValue()<BR>{<BR> var b = new CSMString(" blah ");<BR> alert(b);<BR> alert(b.LTrim());<BR> alert(b.substr(2,2));<BR> alert(b.length); &#060;-- ALWAYS returns 0.<BR>}

  2. #2
    Join Date
    Dec 1969

    Default Crosspost...answered in other forum.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts