gpt4 book ai didi

javascript - Douglas Crockfords - 如何将基方法调用到继承类中

转载 作者:行者123 更新时间:2023-11-29 22:25:29 24 4
gpt4 key购买 nike

我正在尝试使用 Crockford 的继承模式构建基类 Shape。使用这个基本形状,我试图画一个圆、一个矩形和一个三 Angular 形。我有点卡住了。我不知道如何调用/修改基本方法

function points(x,y) {
x = this.x;
y = this.y;
}

function Shape() {
return {
this.points: [ ],

init : function(){
if(typeof this.context === ‘undefined’){
var canvas = document.getElementById(‘canvas’);
var context = canvas.getContext(‘2d’);
}
},
draw: function(){
var context = this.context;
context.beginPath();
context.moveTo(this.points[0].x, this.points[0].y);
for(var i=1; i< this.parameter.length; i++){
context.lineTo(this.parameter[i].x, this.parameter[i].y);
}
context.closePath();
context.stroke();
}
};
}

function Circle(x, y, r){
var points = Shape();
point.x = x;
points.y = y;
points.r = r;
var baseMethod = that.draw;
that.draw = function(){
/*how to modify the base method to draw circle*/
};

}
function Rectangle(a, b, c, d){
var points = Shape();
point.a = a;
points.b = b;
points.c = c;
points.d = d
var baseMethod = that.draw;
that.draw = function(){
/*how to call base method to draw rectangle*/
};

}

最佳答案

您的代码出现了很多问题。首先,您需要确保在继续使用更复杂的形状(例如圆形和矩形)之前,您的基本绘图代码可以正常工作。从画线开始。我已经整理了您的代码并让它可以绘制直线:

//returns basic point object which has
//two properties x & y
function point(x, y) {
return {
x: x,
y: y
}
}


//function that returns a shape object with all the
//mechanisms for drawing lines between points
function Shape(canvasID) {
return {
points: [], //not 'this.points' (which would most likely be window.points)
addPoint: function(x, y) {//adding a point to a shape is an operation of shape
this.points.push(point(x, y))
},
init: function() {
if (typeof this.context === 'undefined') {
var canvas = document.getElementById(canvasID);
var ctx = canvas.getContext('2d');
this.context = ctx; //add the context reference to the current shape object
}
},
draw: function() {
this.init();
var context = this.context;
context.beginPath();
var that = this; //create a local reference to the current 'this' object.
//insures us against any possible 'this' scope problems
context.moveTo(that.points[0].x, that.points[0].y);
for (var i = 1; i < that.points.length; i++) {
context.lineTo(that.points[i].x, this.points[i].y);
}
context.closePath();
context.stroke();
}
};
}

//Simple Line object - good for testing your
//basic drawing functionality
function Line(canvasID, x, y, x2, y2) {
var shape = Shape(canvasID);
shape.addPoint(x, y);
shape.addPoint(x2, y2);
shape.draw();

}

//Execute your drawing functionality after the
//window has loaded to make sure all your objects exist before
//trying to use them
window.onload = function() {
Line('canvas', 100, 100, 200, 200);
}

我不一定相信这是否是处理您正在做的事情的最佳方式 - 但 DC 的基本方法是创建对象而不必使用“new”关键字。因此,他使用 JavaScript 对象表示法从函数调用返回一个对象。

现在您可以画一条线了,下一步是画出一系列连接的线(一条路径)。之后,创建您的矩形。您需要一些代码来告诉您的代码从哪里开始绘制矩形(起始 x/y 坐标),然后您可以使用表示矩形高度和宽度的参数,这些参数将用于计算矩形 Angular 的坐标并通过以与绘制一系列连接线相同的方式绘制到要绘制的形状对象。不过,需要注意的是,检查上下文对象上是否存在某种“createRectangle”函数(对于圆也是如此)。我实际上并不了解自己,因为我没有在 HTML5/canvas 中做过此类工作 - 尽管我在其他环境中做过。

编辑

忘记提及您需要确保您的 html 的文档类型声明是 html5。许多 IDE 会自动将您的 html 声明为 html4。 Html5 只需要:<!DOCTYPE html>

此外,请确保在 html 正文中声明了一个 canvas 元素,如下所示:

<canvas id="canvas" width="300" height="150">   
</canvas>

关于javascript - Douglas Crockfords - 如何将基方法调用到继承类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9742603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com