javascript TypeError:找不到函数startsWith
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6540745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript TypeError: Cannot find function startsWith
提问by Amir Afghani
I have a javascript function:
我有一个 javascript 函数:
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:
当我在 typeof 返回的东西上运行它时object
,它起作用了。当我在 typeof 返回的东西上运行它时string
,我得到:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function startsWith. (<Unknown source>#29) in <Unknown source> at line number 29
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?
有谁知道这是怎么回事?我什至不明白为什么我使用的第一件事的实例类型是 object。这是 readLine 调用的结果,不应该是字符串吗?
<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>
回答by moe
try with capital S :
尝试使用大写 S :
String.prototype.startsWith = function(str) {
return (this.indexOf(str) === 0);
}
回答by salman.mirghasemi
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.
我认为问题在于你starstWith
在定义函数之前调用了它。也许您应该在for
块之前放置两个函数定义。通过这种方式,它们在被调用之前被评估。