JavaScript对象继承问题
JavaScript object inheritance issues
嗨,请耐心等待。我是一名PHP开发人员,正在进入javaScript的世界。现在我正在构建一个包含不同大小块的网格。我想要一个包含所有方法的基块,然后是其他扩展它(使用它的方法)但具有不同属性的块。我读了很多书,但似乎找不到一个连贯的方法。我已经编写了一些代码,我认为它可以工作,但它不起作用。示例中的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对象继承问题
- 类中对象的奇怪问题(John Resig简单继承)
- AngularJS 1.X ES6继承问题
- Javascript继承的问题&基类原型
- 继承问题
- Javascript和原型继承问题
- 使用函数原型的 JavaScript 继承问题
- Javascript原型继承问题
- ng-重复范围问题:两级继承
- 尝试继承for循环中名称引用的所有类时出现问题
- node.js中的继承问题:试图覆盖子类的原型时出现赋值错误
- 原型继承的新手-Can'找不到Undefined变量的问题
- JavaScript对象继承问题
- Backbone.js继承的问题
- 基本javascript继承问题
- Javascript继承问题
- Javascript继承类构造函数问题
- “this”的继承与问题关键字
- Javascript继承设计问题
- 在javascript中继承了DOM元素对象!这段代码有什么问题?