javascript TypeError: Cannot find function startsWith
<p>I have a javascript function:</p>

<pre><code>           string.prototype.startsWith = function(str) {
               return (this.indexOf(str) === 0);
           }
</code></pre>

<p>and when I run it on something whose typeof returns <code>object</code>, it works.  When I run it on something whose typeof returns <code>string</code>, I get:</p>

<p><code>javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function startsWith. (&lt;Unknown source&gt;#29) in &lt;Unknown source&gt; at line number 29
</code></p>

<p>Does anyone know whats going on?  I don't even understand why the instance type of the first thing I'm using is object.  It's the result of a readLine call, shouldn't that be string?</p>

<pre><code>&lt;target name="analyze"&gt;
    &lt;script language="javascript"&gt;

    &lt;![CDATA[
            importClass(java.io.File);
            importClass(java.io.FileReader)
            importClass(java.io.BufferedReader)
            //setup the source directory
            dir = project.getProperty("PROJECT_HOME");
            fs = project.createDataType("fileset");
            fs.setDir(new File(dir));
            //only analyze java files
            fs.setIncludes("**/*.java");
            echo = project.createTask("echo");
            //iterate over files found
            srcFiles = fs.getDirectoryScanner(project).getIncludedFiles();
            for (i = 0; i &lt; srcFiles.length; i++) {
                var filename = srcFiles[i];
                inFile  = new BufferedReader(new FileReader(new File(dir + "/" + filename)));
                while ((line = inFile.readLine()) != null) {
                   if(line.startsWith("package")) {
                       packageStr = line.substr(8);
                       while((line = inFile.readLine()) != null) {
                           if(line.startsWith("import") &amp;&amp; line.endsWith("Foo;")) {
                               //strip out the leading import in 'import x.y.z'
                               var importStr = line.substr(7);
                               importStr = importStr.substr(0, importStr.lastIndexOf('.'));
                               if(!importStr.startsWith(packageStr)) { &lt;---- FAILS!
                               }
                           }
                       }
                   }
                }
                inFile.close();
           }

           String.prototype.startsWith = function(str) {
               return (this.indexOf(str) === 0);
           }

           String.prototype.endsWith = function(str) {
               var lastIndex = this.lastIndexOf(str);
               return (lastIndex != -1) &amp;&amp; (lastIndex + str.length == this.length);
           }

    ]]&gt;
    &lt;/script&gt;
&lt;/target&gt;
</code></pre>
