Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Components.lastResult

Components.lastResult は XPConnect を介した直前の XPCOM メソッド呼び出しの結果コードである数値コード nsresult を返します。

はじめに

Components.lastResult は一般的に、「成功」コードを返す XPCOM メソッドの結果のテストにのみ役立ちます。というのは、失敗した結果コードは XPConnect が例外に変換して、呼び出し元の JavaScript メソッドへ投げるからです。ほとんどのインターフェースは 1 つの成功コード (NS_OK) だけを返すので、Components.lastResult はほとんど必要ありません。

Components.lastResult が利用される場合は、対象の呼び出しの後テストのためにローカル変数に保存することが、複数のテストを Components.lastResult に対して行うよりも適当です。多くの「Components」プロパティとメソッドは XPConnect で実装されていて、それに続く Components.lastResult 呼び出しは対象とした呼び出しでなく「暗黙的な」 XPConnect 呼び出しの結果を返すかもしれないからです。

実例

In the following example, the local variable i contains the actual result returned by bar() (assuming that bar() is called via XPConnect), and Components.lastResult contains the success code returned by bar().

// Given that foo.bar is a method that might return 
// the success codes NS_OK, '5', and '6' OR some error code...
try
{
  var i = foo.bar();

  switch (Components.lastResult)
  {
    case Components.results.NS_OK:
      // NS_OK is good!
      break;
    case 5:
      // do something with 5 here
      break;
    case 6:
      // do something with 6 here
      break;
    default:
      // this was a success code we did not expect. Bad component!
      break;
  }
  // and so on....
}
catch (e)
{
  // the call threw an exception or a native component returned 
  // a failure code!
  if (e instanceof Components.interfaces.nsIXPCException)
  {
    // we might do something interesting here with the exception object
    var rv = e.result;
  }
  else
  {
    // if we don't know how to handle it then rethrow
    throw e;
  }
}

ドキュメントのタグと貢献者

 このページの貢献者: kohei.yoshino
 最終更新者: kohei.yoshino,