有人可以幫助我理解面向對象的JavaScript方法嗎?我習慣寫的js代碼如下:需要幫助瞭解面向對象的Javascript方法
function o_deal(id) {
var hand = gcard1 + ", " + gcard2;
var res = gcard1_val + gcard2_val;
document.getElementById(id).innerHTML = hand;
if (res == 21) {
alert("Blackjack!");
}
if (bucket == 0) {
bucket = " ";
}
var card3_val = Math.floor(Math.random() * deck.length);
var nhand = deck[card3_val];
bucket = bucket + " " + nhand + ", ";
bucket_val = bucket_val + gcard1_val + gcard2_val + card3_val;
if (bucket_val >= 22) {
var r = confirm("Bust! By " + nhand);
if (r == true) {
refresh();
}
else {
refresh();
}
}
document.getElementById(id).innerHTML = bucket;
}
但我已經看到了許多像這樣寫代碼的堆棧溢出海報:
var Hand = function(bjcallback) {
this.cards = [];
this.onblackjack = bjcallback;
this.deck = [1,2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"];
this.values = {
"Jack": 10,
"Queen": 10,
"King": 10,
"Ace": 11
};
this.sum = function() {
var i, x, res = 0;
for (i in this.cards) {
x = this.cards[i];
if (typeof(x) != 'number') { x = this.values[x] };
res += x;
};
return res
};
this.pick = function() {
var pos = Math.floor(Math.random() * this.deck.length);
var card = this.deck[pos];
console.log(card);
return card
};
this.deal = function(n) {
n = n || 2;
可有人請往下突破的第二個方法所以我可以瞭解其中的差異?任何幫助,將不勝感激。
這只是程序與OO。你能更具體地說明你不明白的東西嗎? – Raynos
我完全不理解OO語法。我會指出更多,但我不能遵循代碼。面向對象和程序之間的字面差異是什麼? – dopatraman
你需要比較兩個較大的片段 – Raynos