0

2 つの JavaScript ファイル (file1、file2) があります。File1 は、file2 で定義されたクラスを使用します。次の方法で、html ファイルからこれらのファイルを参照できますか。

<script type="text/javascript" src="file1.js"></script>
<script type="text/javascript" src="file2.js"></script>

これにより、file2 で定義されたクラスへの file1 の依存が可能になりますか? そうでない場合、この種の依存関係を許可するプラグインは何ですか?

4

2 に答える 2

0

それはあなたがそれらを使用しようとしている方法に関係しています。単純化されたアプローチ。

シナリオ 1:

script1.js

function primary()
{
    secondary();
}

script2.js

function secondary()
{
    alert("hi primary");
}

test.html

<html>
<head>
<script src=script1.js></script>
<script src=script2.js></script>
</head>
<body>
</body>
</html>

それはうまくいきます(あなたはすでにそれを知っています)

シナリオ 2:

script1.js

secondary();

上記の script2.js と test.html

動かない(jsエラー)

シナリオ 3:

script1.js

secondary();

script2.js は同じままです

test.html

<html>
<head>
<script src=script1.js defer></script>
<script src=script2.js></script>
</head>
<body>
</body>
</html>

できます。

これはあなたが探しているものですか?

于 2012-06-13T22:24:38.850 に答える