2012-05-01 23 views
1

在Chrome開發者控制檯中,鍵入$x.toString()$x是開發工具控制檯的內置函數之一)。輸出如下:如果一個函數的toString()以「bound:」開頭,這是什麼意思?

"bound: function (xpath, context) 
    { 
     var doc = (context && context.ownerDocument) || inspectedWindow.document; 
     var result = doc.evaluate(xpath, context || doc, null, XPathResult.ANY_TYPE, null); 
     switch (result.resultType) { 
     case XPathResult.NUMBER_TYPE: 
      return result.numberValue; 
     case XPathResult.STRING_TYPE: 
      return result.stringValue; 
     case XPathResult.BOOLEAN_TYPE: 
      return result.booleanValue; 
     default: 
      var nodes = []; 
      var node; 
      while (node = result.iterateNext()) 
       nodes.push(node); 
      return nodes; 
     } 
    }" 

「bound:」在第一行中的含義是什麼?

回答

4

$x是webkit Developer Tools控制檯中的內建函數,如$,$$和其他。該CommandLineAPI(用於控制檯腳本評價)將覆蓋所有控制檯方法toString功能包括"bound: "前綴:

function bind(thisObject, memberFunction) 
{ 
    var func = memberFunction; 
    var args = Array.prototype.slice.call(arguments, 2); 
    function bound() 
    { 
     return func.apply(thisObject, args.concat(Array.prototype.slice.call(arguments, 0))); 
    } 
    bound.toString = function() { 
     return "bound: " + func; 
    }; 
    return bound; 
} 

的包裹這樣的控制檯功能的完整列表,可以發現here

+0

大+1!謝謝你,這讓我瘋狂。 –

+0

謝謝 - 現在我想知道的是,爲什麼他們會想要這樣做。 –

相關問題