SSブログ

[Note] 厄介な JavaScript コメント - Troublesome JavaScript comment [Internet Explorer]

ある Web システムで、IE8、IE11、iPad ではちゃんと動くんですが。 

A certain Web system worked on IE8, IE11, or iPad.

IE9 と IE10 だけ動きませんでした。 

But it didn't work on IE9, or IE10.

なんで? なにが起きた? どういうコードになってるの? 

Why? What's happened? How was it coded?

例外をキャッチして分析してみたら、JavaScript ファイルの中に、以下のような一連のコメントがありました。 

I caught the exception, and analyzed it, then I found that there are a series of comments in a javascript file as following;

/*@cc_on
var doc = document;
eval('var document = doc');
@*/

この一連のコメントはよく知られたテクニックで、スクリプトの高速化のために使われます。 

These a series of comments are well known technique, they are used for the speedup of the script.

IE8 では、これらのコメントは JScript の @cc_on ステートを以ってスクリプトに解釈され、そのコードが window.document オブジェクトの再定義を行います。 

On IE8, the series of comments is parsed to script with "@cc_on" statement of the "JScript", and the parsed codes redefine the window.document object with "eval" function.

これでスクリプトが高速になるんです。 

It makes the script faster.

しかし、IE9 と IE10 では、この一連のコメントは IE8 同様にスクリプトに解釈されますが、window.document オブジェクトを再定義しようとした時点で例外がスローされます。

However, On IE9 or IE10, the series of comments is parsed to script with "@cc_on" statement same as IE8, but the exception is thrown when the window.document object is redefined.

IE9 と IE10 では、window.document オブジェクトは const として定義されているので、再定義できないんです。

On IE9 and IE10, the window.document object has been defined as "const", it's impossible to be redefined.

IE11 の場合、これらのコメント自体が解釈されません。IE11 が @cc_on をサポートしていないためです。 

On IE11, the series of comments is NOT parsed, because IE11 does NOT support the "@cc_on" statement.

なので、 window.document オブジェクトの再定義は実行されません。

Then, redefining of the window.document object never be done.

iPad の場合、Microsoft の製品ではないので、JScript も "@cc_on" もわかりません。 

On iPad, Safari Mobile knows neither JScript nor "@cc_on", because it is not Microsoft's products.

これがすべてです。 

That's the reason at all.

このテクニックはおもしろいし、特定の局面ではふさわしい解決方法になりえると確信しますが。 

I think that this technique is interesting, and I'm sure that it can become a good solution in the special situation.

でも、もしこう独特のテクニックを使う場合は、 使う側の責務として、動作要件に最大限の注意を払うべきと思います。

But, when we use an odd technique such as this, we must pay the greatest attention to operating requirements.

ちなみにこのコード、あってもなくても変わりませんでした。 

By the way, with or without this technique, the script execution speed did not change with IE8.

そもそもいらねーじゃんというお話です。 

It was unnecessary implementation, in the first place.


この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。