所以我是编程新手,正在尝试学习javascript,html和css。我使用Firebug查看代码并尝试理解它。找到了某人 " Hangman Game"这看起来很酷。
然而,当我试图通过Firebug看到它是如何完成的时候,我看到了#34;奇怪的角色"这使得很难真正理解一切。看起来有些东西具有相同的变量,即使它们不同。
所以想知道是否有可能对此采取任何措施?
代码看起来像这样
window.Hangman = function () {
'use strict';
var e = {
partAsElement: {
hill: document.getElementById('hang_hill'),
gallow: document.getElementById('hang_construction'),
body: document.getElementById('hang_body'),
rightarm: document.getElementById('hang_rightarm'),
leftarm: document.getElementById('hang_leftarm'),
rightleg: document.getElementById('hang_rightleg'),
leftleg: document.getElementById('hang_leftleg'),
rope: document.getElementById('hang_rope'),
head: document.getElementById('hang_head')
},
validParts: [
'hill',
'gallow',
'rope',
'head',
'body',
'rightarm',
'leftarm',
'rightleg',
'leftleg'
],
ordlista: [
'FANTASY',
'NOTHING',
'INDIGO',
'NATIVE',
'FOREARM',
'NODE',
'EYESOCKET'
],
strecklista: [
],
usedlista: [
],
k: 0,
isValid: function (e) {
return - 1 === this.validParts.indexOf(e) ? (console.log('The part is not valid: ' + e), !1) : (console.log('The part is valid: ' + e), !0)
},
hide: function (e) {
this.isValid(e) && (console.log('Hiding part: ' + e), this.partAsElement[e].style.display = 'none')
},
show: function (e) {
this.isValid(e) && (console.log('Showing part: ' + e), this.partAsElement[e].style.display = 'inline')
},
shuffle: function (e) {
var t,
n,
l;
for (l = e.length; l; l -= 1) t = Math.floor(Math.random() * l),
n = e[l - 1],
e[l - 1] = e[t],
e[t] = n
},
pickWord: function () {
var e;
return this.shuffle(this.ordlista),
e = this.ordlista[0],
console.log(e),
e
},
drawUnderscore: function (e) {
var t,
n,
l;
for (console.log(e), t = document.getElementById('streck'), n = 0; n < e.length; n++) this.strecklista.push('_');
return l = this.strecklista.join(' '),
t.innerHTML = l,
l
},
'klickaVälj': function (n) {
var l,
o,
s,
a,
i,
r;
l = document.getElementsByClassName('bokstav'),
s = document.getElementById('streck'),
a = document.getElementById('used'),
l[n].addEventListener('click', function () {
for (l[n].classList.add('selected'), o = this.innerHTML, i = 0, r = 0, n = 0; n < t.length; n++) t[n] === o ? (console.log(t[n]), e.strecklista[n] = o) : (console.log('bokstaven finns inte på plats ' + n), i++);
for (console.log('j = ' + i), console.log(e.strecklista), n = 0; n < e.strecklista.length; n++) '_' === e.strecklista[n] && (r++, console.log(r));
s.innerHTML = e.strecklista.join(' '),
0 === r && window.alert('You won!'),
console.log(e.k),
i === t.length && (console.log('utför andra if-satsen'), e.usedlista.push(o), console.log('usedlista' + e.usedlista), e.show(e.validParts[e.k]), e.k++, e.k === e.validParts.length && window.alert('You lost!')),
a.innerHTML = e.usedlista
})
},
addClick: function () {
var e;
e = document.getElementsByClassName('bokstav');
for (var t = 0; t < e.length; t++) this.klickaVälj(t)
}
},
t = e.pickWord();
return e.drawUnderscore(t),
console.log(t),
console.log('You can now use the hangman object as a part of the window-object. Try\n\nwindow.Hangman.hide(\'gallow\')\nwindow.Hangman.show(\'gallow\')\n\nHere are all the parts you can work on.'),
console.log(e.validParts),
e
}();
编辑:https://jsfiddle.net/8c34noqf/(从我可以看到他们使用2个js文件,我只能粘贴其中一个)。所以这是另一个
!function () {
'use strict';
function n() {
for (a = 0; a < i.length; a++) window.Hangman.hide(i[a])
}
var a,
i;
i = window.Hangman.validParts,
n(),
window.Hangman.addClick()
}();