javascript - JS : Refactoring Code into OOP

标签 javascript oop object refactoring structure

我编写了一个不完全 OOP 的应用程序。我正在考虑将其转换为“真正的”OOP。

现在,代码是通过将子函数设置为主函数的属性来构造的。

例如:Bullet.move.normal是一个子函数/依赖项或Bullet.move

这就是代码现在的样子:

Bullet = function(x,y,angle,type){
    return {x:x,y:y,angle:angle,type:type};
}

Bullet.update = function(b){
    Bullet.move(b);
    Bullet.collision(b);
}

Bullet.move = function(b){
    if(b.type === 'normal') Bullet.move.normal(b);
    else if(b.type === 'sinus') Bullet.move.sinus(b);
}

Bullet.move.normal = function(b){
    b.x +=  Math.cos(b.angle);  b.y +=  Math.sin(b.angle);  //not important
}   
Bullet.move.sinus = function(b){
    b.x +=  Math.cos(b.x);  b.y +=  Math.sin(b.y); //not important
}   

Bullet.collision = function(b){
    Bullet.collision.wall(b);
    Bullet.collision.actor(b);
}

Bullet.collision.wall = function(b){} 
Bullet.collision.actor = function(b){}


---

我已经开始编写上面代码的 OOP 版本,但是我的结构不能完美地工作。

如果它是“多级”函数,

this 参数不会引用该对象。 (检查Bullet.prototype.move.normal)

在不必将所有子函数放入主函数中的情况下,推荐的重构原型(prototype)的方法是什么? (检查第二个Bullet.prototype.move)

除了像 Bullet.prototype.move_normal 这样命名所有内容之外,还有其他解决方案吗?我不希望所有东西都处于同一“级别”。

使用 OOP 代替我以前的方法有什么优势?是否值得转换为 OOP?

Bullet = function(x,y,angle,type){
    this.x = x; 
    this.y = y;
    this.angle = angle;
    this.type = type;
}

Bullet.prototype.update = function(){
    this.move();
    this.collision();
}

Bullet.prototype.move = function(){
    if(this.type === 'normal') this.move.normal();
    else if(this.type === 'sinus') this.move.sinus();
}

Bullet.prototype.move.normal = function(){  
    //not working, this === Bullet.prototype.move, not the bullet
    this.x +=  Math.cos(this.angle);    //not important
    this.y +=  Math.sin(this.angle);    
}


Bullet.prototype.move = function(){ //I dont want this. I'd like to keep things separated.
    if(this.type === 'normal'){
        this.x +=  Math.cos(this.angle);
        this.y +=  Math.sin(this.angle);    
    }
    else if(this.type === 'sinus'){
        this.x +=  Math.cos(this.x);
        this.y +=  Math.sin(this.y);    
    }
}

最佳答案

用子类替换类型代码将是一个很好的起点:

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

Bullet = function(x, y, angle, type){
    this.x = x;
    this.y = y;
    this.angle = angle;
    this.type = type;
};

Bullet.prototype.update = function(){
    this.move();
    this.collision();
};

Bullet.prototype.collision = function(b){
    this.collisionWall(b);
    this.collisionActor(b);
};

Bullet.prototype.collisionWall = function(b){};
Bullet.prototype.collisionActor = function(b){};

//NormalBullet
NormalBullet = function() {
    //Call parent constructor and pass all the arguments in.
    Bullet.apply(this, arguments);
};

//Set prototype inheritance.
extend(Bullet, NormalBullet);

//Move the bullet move logic into subclass.
NormalBullet.prototype.move = function() {
    this.x +=  Math.cos(this.angle);
    this.y +=  Math.sin(this.angle);
};

//SinusBullet
SinusBullet = function() {
    Bullet.apply(this, arguments);
};

extend(Bullet, SinusBullet);

SinusBullet.prototype.move = function() {
    this.x +=  Math.cos(this.x);
    this.y +=  Math.sin(this.y);
};

var sinusBullet = new SinusBullet(//your arguments);
sinusBullet.move();

var normalBullet = new NormalBullet(//your arguments);
normalBullet.move();

Source

关于javascript - JS : Refactoring Code into OOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24255453/

相关文章:

javascript - 如何检查 YouTube 视频是否允许嵌入播放

java - Java中的多项——可重用性和可维护性

java - 如何在java中读取对象并将其写入文本文件?

javascript - 我可以从嵌套对象创建新的 Javascript 对象吗?

c++ - 意外无法访问的基础(第一个派生类)

php - 如何从foreach循环php中的对象数组中删除对象

javascript - 使用请求 API 数据的函数时出错

javascript - js/jQuery 可以确定 iPhone 的方向吗?

javascript - 从 bootstrap 下拉列表中获取选定的文本

java - 访问外部类这个实例