首先,我们来看一段代码,如何觉得不甚明白的,则本文会对你有益:
var player = function (e) {
return (function f(m) {
return m ? (function (n) {
return $('#Player', n).get(0) || f($('iframe', n).get(0));
})(m.contentWindow.document) : null;
})($(e).get(0));
};
该段代码涉及到的知识点包括:方法、匿名方法、匿名方法的立即执行、JQuery对象及Dom对象的互转等。现在,我们回过头来从基础的开发,争取对于JavaScript中的方法、方法引用和参数弄得概念清楚一些。
一:示例代码
首先,先看测试代码:
<!DOCTYPE html>
<head>
</head>
<body>
<script src="scripts/jquery.min.js"></script>
<script language="javascript">
window.console.log("=================================");
var p0 = function (e) {
window.console.log("self:" + e);
};
p0("outp0");
var A = new p0("p0");
window.console.log("=================================");
var p00 = function (e) {
window.console.log("self:" + e);
return function f(m) {
window.console.log("newo:" + m);
};
};
var B = new p00("outp00B");
B("BB");
var C = new B("outp00C");
window.console.log("=================================");
var p01 = function (e) {
window.console.log("self:" + e);
return (function f(m) {
window.console.log("newo:" + m);
})("p01");
};
var D = new p01("outp01D");
window.console.log("=================================");
var p1 = (function (e) {
window.console.log("self:" + e);
})("p1");
window.console.log("=================================");
var p11 = (function (e) {
window.console.log("self:" + e);
return function (m) {
window.console.log("newo:" + m + e);
};
})("p11");
p11("outp11_2");
var E = new p11("outp11");
window.console.log("=================================");
var p12 = (function (e) {
window.console.log("self:" + e);
return {
p121: function (m) { window.console.log("newo:" + m); },
p122: function (options, fn) {
fn(e + options);
}
};
})("p12");
p12.p121("outp121");
p12.p122("outp122", function (e) {
window.console.log("invoker:" + e)
});
</script>
</body>
</html>
查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/