文字列出力のラッパー

ブラウザ側とシェル(SpiderMonkeyJScript.NET)での文字列出力の実装が違うので(まあ当然だけど。)SSとCSで互換の文字列出力のクロス関数書いてみた。

その1

global = this;
Print = function()
{
  if(global.document)
     return document.write;
  return print;
}(); // 定義と同時に呼び出してるので中身は document.write や printのエイリアス

これだとIEだと大丈夫だけどFirefoxだと全然駄目。なんかエラーが出てくる。しかたないのでクロージャー書いてラッパーをつくる

その2

Print = function()
{
   if(global.document)
      return function(str){ document.write(str) };
   return function(str){ print(str) };
}(); // 定義と同時呼び出しなので Printの中身は既に決まっている。

その3

Print = function(str)
{
   if(global.document)
      document.write(str);
   else
      print(str);
};

その3みたいな書き方だと毎回毎回どっちを使うか選ぶからきっとその2のほうが処理効率としてはいいのだろう(ベンチマークとってないので勘)。ちなみにWSHではWScript.Echoを利用する。