gpt4 book ai didi

JavaScript 对象继承问题

转载 作者:行者123 更新时间:2023-12-03 06:57:35 24 4
gpt4 key购买 nike

嗨,请耐心等待我。我是一名进入 JavaScript 世界的 PHP 开发人员。现在我正在构建一个包含不同大小的 block 的网格。我想要一个包含所有方法的基本 block ,然后是扩展它的其他 block (以使用其方法)但具有不同的属性。我读了很多书,但似乎无法找到一致的方法。我已经整理了一些代码,虽然我可以工作,但事实并非如此。示例中的 SmallBlock 有自己的属性,但没有 Blocks 属性

    // Simple extend function to add for creating object inheritance
function extend(Child, Parent) {
var Temp = function(){
Temp.prototype = Parent.prototype;
Child.prototype = new Temp();
Child.prototype.constructor = new Child();
}
}

//__________________________________________________
// ## OBJECT DECLARATIONS ##
//__________________________________________________

//__________________________________________________
// ## BLOCK ##
// Base object representing a block/tile in the grid
var Block = function(){

// The X position block
this.positionX = 0;

// The Y position block
this.positionY = 0;

// The height of the block
this.height = 0;

// The width of the block
this.width = 0;

// The horizontal orientation of a block
this.blockHorizontalOrientation = null;

// The vertical orientation of a block
this.blockVerticalOrientation = null;

// Method to set the width of a block
this.setWidth = function(width){
this.width = width;
};

// Method to set the height of a block
this.setHeight = function(height){
this.height = height;
};

// Method to get the width of a block
this.getWidth = function(){
return this.width;
};

// Method to get the height of a block
this.getHeight = function(){
return this.height;
};

// Method to set the X position of a block (in the grid)
this.setPosX = function(posX){
this.positionX = posX;
};

// Method to set the Y position of the block (in the grid)
this.setPosY = function(posY){
this.positionY = posY;
};

// Method to get the Y position of the clock
this.getPosY = function(){
return this.positionY;
};

// Method to get the X position of the clock
this.getPosX = function(){
return this.positionX;
};

// Method to get the horizontal orientation
this.getHOrientation = function(){
return this.blockHorizontalOrientation;
};

// Method to get the vertical orientation
this.getVOrientation = function(){
return this.blockVerticalOrientation;
};

// Method to get the horizontal orientation
this.setHOrientation = function(hOren){
this.blockHorizontalOrientation = hOren;
};

// Method to get the vertical orientation
this.setVOrientation = function(vOren){
this.blockVerticalOrientation = vOren;
};
}

//__________________________________________________
// ## SMALL BLOCK ##
// object representing a small block/tile in the grid -- extends Block
var BlockSmall = function(){

// The X position block
this.positionX = 0;

// The Y position block
this.positionY = 0;

// The height of the block
this.height = 1;

// The width of the block
this.width = 1;

// The horizontal orientation of a block
this.blockHorizontalOrientation = null;

// The vertical orientation of a block
this.blockVerticalOrientation = null;
};

// EXTENDING BLOCK
extend(BlockSmall, Block);

请有人检查一下这段代码并通过建议我做错了什么来提供帮助。如果我在几英里之外,请再次耐心等待。这对我来说都是全新的概念。

最佳答案

嗯,这应该适用于继承:

function extend(Child, Parent) {
Child.prototype = new Parent();
Child.constructor = Child;
}

在您的代码中,您只需在 extend 中定义一个永远不会被调用的函数。

关于JavaScript 对象继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37207284/

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