0

我正在编写简单的 javascript。脚本是

document.write("Before script");
var test=new java.lang.String();
test="Test"
document.write(test);

对于上面的脚本得到以下错误

Uncaught ReferenceError: java is not defined

可能是什么问题?

4

3 回答 3

1

你不能做这样的声明:

var test=new java.lang.String();

在 JavaScript 中。Javascript 不共享 Java API,它是一种完全不同的语言。

于 2012-09-20T18:53:56.970 回答
0

You cannot access Java objects (without some other in between) from JavaScript since they are two different languages. It should also be noted "var" does not stand for "variable" but rather for "variant" since it can hold many different types in itself. This means you don't need to create a new string or int before setting it.

于 2012-09-20T17:55:39.037 回答
0

JavaScript, despite the name, is not a scripting language for Java. They have little relation to each other besides the syntax. You'll have to learn a new language to write JavaScript programs.

Your script in particular should be:

document.write("Before script");
var test = "Test";
document.write(test);
于 2012-09-20T17:56:13.130 回答