1

I have a javascript function:

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

and when I run it on something whose typeof returns object, it works. When I run it on something whose typeof returns string, I get:

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

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?

<target name="analyze">
    <script language="javascript">

    <![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 < 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") && 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)) { <---- 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) && (lastIndex + str.length == this.length);
           }

    ]]>
    </script>
</target>

2 Answers 2

3

I think the problem is that you call starstWith function before you define it. Perhaps you should put two function definitions ahead of for block. In this way they are evaluated before they are called.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it! javascript sucks.
2

try with capital S :

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

1 Comment

Sorry I should have mentioned that I did both and it didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.