嘿伙计们,我处理这种“模式”已经有一段时间了,但我无法欣赏这种架构选择。在我看来,代码很糟糕而且毫无意义。
我附上了一个代码示例,以便更清楚地说明。
所有这些在文件之外声明的 IIFE 是否都准备好了?这是一种模式还是只是 Spaghetti JS?任何弱点或架构错误?
索引.html
<html>
<head>
<meta HTTP-EQUIV='content-type' CONTENT='text/html; charset=utf-8'/>
</head>
<body>
<div id="first"></div>
<div id="second" style="border:2px solid green;width:150px;height:190px;"></div>
</body>
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script>
<script type='text/javascript' src='js/scope.js'></script>
</html>
范围.js
(function() {
if (typeof $M === 'undefined') { $M = {}; }
var
$document = $(document);
$first = $('#first'),
$second = $('#second'),
$chunk = $("<div id='chunk'> truffle shuffle </div>"),
documentHeight = $document.height(),
animationTime = 1000,
style = {
'border':'2px solid red',
'height': documentHeight / 8,
'width': '150px'
},
style2 = {
'height': documentHeight / 4,
'width': '300px'
};
var init = function() {
$second.hide(); // init ops
}
function appendChunk() {
$first.append($chunk);
$chunk.css(style);
}
function animateChunk() {
$chunk.animate(style2,animationTime,function(){
$(this).trigger('animationComplete');
});
}
appendChunk();
animateChunk();
$M.one = init;
})();
(function() {
if (typeof $M === 'undefined') { $M = {}; }
var
$second = $('#second'),
$chunk = $("#chunk"),
animationTime = 1000,
style = {
'border':'2px solid red',
'height': '150px',
'width': '150px'
};
var init = function() {
$second.hide(); // init ops
}
$chunk.on('animationComplete',function(){
$second.fadeIn().trigger('animationComplete');
});
$second.on('animationComplete',function(){
$chunk.animate(style,animationTime);
});
var time = setInterval(function() {
if($second.is(':visible')) {
console.log("visible");
clearInterval(time);
} else {
$second.html("finished!");
}
},200);
$M.two = init;
})();
$(document).ready(function () {
$M.one();
$M.two();
});