2013-10-03 7 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? 

だから、私の質問は何に使用されていること:belowedとして

のコード例はありますか? のメリットは何ですか?

FYI、全体のソースコードは、ここでは、あなたが使用されている)注釈の多くを(見ることができる場所である

自家製強い型付けのセットアップのように見える

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)")を実行し、文字列を受け取り、NULL可能なintegerの関数、または文字列のみを受け入れる関数を受け入れることができます。

+0

ありがとう。 matches()関数(ソースコード内でエイリアスが__t)は、関数型の型チェックを行うことです。しかし、この質問は__wの目的を尋ねることです。 @SeanVieira – user1481096

+0

@ user1481096 - 私は 'annotate'についてもう少し説明を追加しました。助けるためにさらに何かできるかどうか私に教えてください! –

+0

スタティックではない(少なくともこの部分ではない)が、強力なダイナミックタイピング。 –

関連する問題