-1
正如話題自己說的,這是JavaScript中的戰略模式的例子嗎?例如,這種戰略模式?
(我認爲問題是更適合代碼審查,但他們重定向我的計算器)
var canvas = {
context: document.getElementById("canvas").getContext("2d")
};
///////////////////////////////////////////////////
function Square(_strategy) {
this.x = 50;
this.y = 50;
this.width = 100;
this.height = 100;
this.strategy = _strategy;
}
Square.prototype.draw = function() {
this.strategy(this);
};
///////////////////////////////////////////////////
function Circle(strategy) {
Square.call(this, strategy);
this.radius = 25;
}
Circle.prototype = new Square();
///////////////////////////////////////////////////
function drawSquare(shape) {
canvas.context.strokeRect(shape.x, shape.y, shape.width, shape.height);
}
///////////////////////////////////////////////////
function drawCircle(shape) {
canvas.context.beginPath();
canvas.context.arc(shape.x , shape.y, shape.radius, 0, 360, false);
canvas.context.stroke();
}
///////////////////////////////////////////////////
var shapes = [];
shapes.push(new Square(drawSquare));
shapes.push(new Circle(drawCircle));
for(var i=0; i<shapes.length; i++) {
shapes[i].draw();
}
我知道,我並不需要的寬度和高度繪製圓。 (我稍後需要它們來選擇那個圓圈,所以它不是錯誤的:))
如果您的投票請至少留下解釋爲什麼... – Srle
我沒有投票,但這個問題沒有表現出任何努力,也沒有表現出對主題的任何理解。你基本上貼了一段代碼,並說:「_true或false,這使用模式X_」。 – jahroy
我認爲這不是代碼的「牆」,例子就足以被審查幾分鐘。無論如何,謝謝你的建議。 – Srle