我正在尝试编写一个简单的Facebook应用程序.在我需要的代码中,我发现了这个:
// Load the SDK asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
Run Code Online (Sandbox Code Playgroud)
为什么这个函数被包裹在()中并且(文档)最后被打了?我以前没见过这种JavaScript法术.
感谢您的任何意见.
它是一个自动执行的函数,document作为参数传递.
这可以改写为:
var myFunc = function(d) {
// code here
};
myFunc(document);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
121 次 |
| 最近记录: |