0

我没有从 facebook 开发者网站获取这段代码中的一些语法。第一个变量“js”和“id”是否以某种方式绑定?第一个 if 语句中究竟返回了什么?

     (function(d){
       var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
       js = d.createElement('script'); js.id = id; js.async = true;
       js.src = "//connect.facebook.net/en_US/all.js";
       d.getElementsByTagName('head')[0].appendChild(js);
     }(document));

这是页面的链接:http: //developers.facebook.com/docs/guides/web/#personalization

4

2 回答 2

1

“绑定”是什么意思?

它们是功能本地的;js是在第 3 行和第 4 行设置的变量,id立即设置为字符串。

函数本身在定义后立即执行,并d设置为document函数内部。

第一个(也是唯一显式的)return语句不返回任何内容。如果这是唯一的代码,那么返回值将毫无意义,因为没有任何东西可以捕获返回值。

于 2012-04-30T14:56:37.317 回答
0

这里没有任何东西,只是被盗的空格:)

(function(d){
    var js,    // variable (empty)
        id = 'facebook-jssdk';    // variable with string 'facebook-jssdk'

    if (d.getElementById(id)) {    // is there an element with id 'facebook-jssdk'
        return;    // yes, so we have nothing to do and get out of here
    }

    js = d.createElement('script');    // create 'script' element
    js.id = id;    // assign id 'facebook-jssdk'
    js.async = true;    // load in "background" (if supported)
    js.src = "//connect.facebook.net/en_US/all.js";    // set source (with the appropriate protocol; https if called via https, http otherwise)
    d.getElementByTagNam('head')[0].appendChild(js);    // append to first head element on page
}(document))    // immediately call the anonymous function and hand in the 'document'
于 2012-04-30T17:21:25.600 回答