2013-10-03 22 views
8

我開始使用Facebook javscript SDK,當我閱讀源代碼時,我發現了一件有趣的事情。Facebook JavaScript SDK中的函數註釋方法的用途是什麼?

的代碼示例是講座敬愛的:那麼,請問這是用於

/** 
* Annotates a function with a meta object 
*/ 
function annotate(fn, meta) { 
    meta.superClass = fn.__superConstructor__; 
    fn.__TCmeta = meta; 
    return fn; 
} 

// export to global 
__w = annotate; 

/** 
* when using the annotate function 
*/ 
function sprintf(format) { 
    // function body 
} 
__w(sprintf, {"signature":"function(string)"}); // <-- what is the purpose of doing this? 

我的問題是什麼? 好處這樣做是什麼?

僅供參考,整個源代碼是在這裏,在這裏你可以看到很多註釋的()正在使用

http://connect.facebook.net/en_US/all/debug.js

+6

不確定爲什麼投票結果和近距離投票,這是一個有趣的問題,並在SO的範圍內。我也很好奇Facebook爲什麼這樣做。 –

+2

如果有人希望我擴大已提到的內容,我很樂意:) –

回答

3

這似乎是一個土生土長的強類型設置:

/** 
* A recursive descent analyzer which takes a value and a typehint, validating 
* whether or not the value matches the typehint. 
* The function will call it self as long as both the value and the typehint 
* yields a nested component. This means that we will never recurse deeper 
* than needed, and also that we automatically get support for 
* > equals([], 'array<string>') // true 
* > equals(['string'], 'array') // true 
*/ 
function equals(value, node) { 
    var type = typeof value; 
    var subType; 
    var nextNode; 
    var nextValue; 

    //: Nullable types are delimited with a leading ? 
    //: ?string, ?boolean, etc. 
    var nullable = /^\?/.test(node); 
    if (nullable) { 
    node = node.substring(1); 
    } 

//: snip ... 

switch (type) { 
    // start by testing the most common types 
    case 'boolean': 
    case 'number': 
    case 'string': 
    case 'undefined': 
    break; 
    default: 
     //: snip ... 
     // let functions with signatures also match 'function' 
     type = value.__TCmeta && node !== 'function' 
     ? value.__TCmeta.signature 
     : 'function'; 
    } else if (type === 'object' || type === 'function') { 
     // HTMLObjectElements has a typeof function in FF 
     var constructor = value.constructor; 
     if (constructor && constructor.__TCmeta) { 
     // The value is a custom type 
     //: snip ... 
      while (constructor && constructor.__TCmeta) { 
      if (constructor.__TCmeta.type == node) { 
       type = node; 
       break; 
      } 
      constructor = constructor.__TCmeta.superClass; 
      } 
     //: snip ... 
     } 
     } 
    } 
} 

if (nullable && /undefined|null/.test(type)) { 
    return true; 
} 

if (type in typeInterfaces) { 
    var interfaces = typeInterfaces[type], i = interfaces.length; 
    while (i--) { 
    if (interfaces[i] === node) { 
     type = node; 
     break; 
    } 
    } 
} 

currentType.push(type); 
return nextValue && nextNode 
    ? node === type && equals(nextValue, nextNode) 
    : subType && nextNode 
    ? node === type && subType === nextNode 
    : node === type; 
} 

 

/** 
* Given a value and a typehint (can be a union type), this will return 
* whether or not the passed in value matches the typehint. 
*/ 
function matches(value, node) { 
    var nodes = node.split('|'), i = nodes.length; 
    while (i--) { 
    currentType = []; 
    if (equals(value, nodes[i])) { 
     return true; 
    } 
    } 
    return false; 
} 

他們之所以使用annotate功能是允許定製類型和功能簽名的類型提示。沒有annotate你只能做matches(someVar, "function")。使用annotate您可以執行matches(someVar, "function(string, ?int)|function(string)"),並且只接受帶有字符串和空值的函數或只接受字符串的函數。

+0

謝謝。 matches()函數(其別名是源代碼中的__t)是執行函數參數類型檢查。但這個問題是要問__w的目的。 @SeanVieira – user1481096

+0

@ user1481096 - 我在'annotate'附加了一些解釋。讓我知道如果我可以做任何進一步的協助! –

+0

不是靜態的(至少不是這個部分),而是強大的動態類型。 –

相關問題