我们有Javascript库:
(function (global, factory) {
factory(global);
}(window , function() {
var MyLib = function(elem) {
return new MyLib.foo.init(elem);
}
MyLib.foo = {
init: function(elem) {
elem = typeof elem == 'string' ? window.document.getElementById(elem) : elem;
this[0] = elem;
return this;
},
test1: function() { // (1)
window.console.log('test1 ' + this[0].nodeName);
this[0].innerHTML = this[0].nodeName;
},
};
MyLib.foo.init.prototype = MyLib.foo;
MyLib.test2 = function() { // (2)
window.console.log('test2');
}
window.MyLib = window.ml = MyLib;
}));
function outerFunc(elem) {
ml(elem).test1();
ml.test2();
}

<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript" src="example.js"></script>
<div onclick="outerFunc(this)">button</div>
</body>
</html>
&#13;
我们什么时候应该创建像(1)和类似(2)这样的函数?
当然,如果需要获取元素,我们总是使用 (1) ,如MyLib()。myFunc(),am我没错?
对于所有情况,如果元素不受影响,我们应该创建 (2) ,例如MyLib.myFunc()?
可能是我们错过了一些变量&#39;保护?