2016-04-22 52 views
1

我有一个例子中的物体像这样:在对象中追加或编辑函数的最快方法是什么?

var shooter = {fire : function(){shootRightHandGun("shotgun");}} 

但是说射手找到了新枪,我们要设置射手火功能,这一点:

{fire : function(){shootRightHandGun("shotgun"); shootLeftHandGun("handgun");}} 

什么是最好的/最快如何实现这一目标? Helper函数,对象中的函数数组,任何东西。我完全接受建议。

回答

2

从技术上讲,你可以通过.toString()方法获得功能的源代码编辑功能,然后执行字符串操作就可以了使用正则表达式等。但它会非常非常混乱,我不推荐它。

相反,给对象更多的结构。首先分离出右手和左手武器:

var shooter = { 
    rightHand : function() {}, 
    leftHand : function() {}, 
    fire : function() {} 
} 

现在让.fire()方法拍摄(或使用)所有武器:

var shooter = { 
    rightHandWeapon : function() {}, 
    leftHandWeapon : function() {}, 
    fire : function() { 
     this.rightHandWeapon(); 
     this.leftHandWeapon(); 
    } 
} 

现在,上面的代码什么也不做(因为这两个函数什么都不做)这意味着上面的代码适用于手无寸铁的射手。

现在,您可以实现武器功能:

function shotgun() { 
    /* code to fire shotgun */ 
} 

function handgun() { 
    /* code to fire handgun */ 
} 

是完整的,我们可以定义以下功能,以及:

function unarmed() {}; 

现在,您可以通过给他的武器武装的射手:

// Armed with shotgun 
shooter.rightHandWeapon = shotgun; 
shooter.fire(); 

// Armed with shotgun and handgun: 
shooter.rightHandWeapon = shotgun; 
shooter.leftHandWeapon = handgun; 
shooter.fire(); 

// Armed with TWO shotguns: 
shooter.rightHandWeapon = shotgun; 
shooter.leftHandWeapon = shotgun; 
shooter.fire(); 

// Disarm the shooter: 
shooter.rightHandWeapon = unarmed; 
shooter.leftHandWeapon = unarmed; 
shooter.fire(); // does nothing 
2

要追加

var temp = shooter.fire; 
shooter.fire = (function(t){ 
    return function(){ 
    t(); 
    shootLeftHandGun("handgun"); 
    } 
})(temp); 

编辑

shooter.fire = function(){shootLeftHandGun("handgun");}; 

小提琴https://jsfiddle.net/trex005/4rhq7zxj/

3

你不应该更换功能为,而不是跟踪项目的单独,它从长远来看可能会对你有所帮助。
您可能需要在代码中的不同位置参考射手掌握的当前项目,进行各种检查和验证。
不要只是替换这样的功能。

var shooter = { 
    leftGun: null, 
    rightGun: "shotgun", 

    fire: function() { 
     if(this.rightGun != null) { 
      shootRightHandGun(this.rightGun); 
     } 
     if(this.leftGun != null) { 
      shootLeftHandGun(this.leftGun); 
     } 
    } 
} 

shooter.leftGun = "handgun"; 

以后,您可以轻松地扩展以适当的getter和setter方法的代码,很容易地添加一些额外的检查等:

getRightGun: function() { return this.rightGun; } 
setRightGun: function(newRightGun) { 
    if(isProperGun(newRightGun)) {    // some kind of a check 
     this.rightGun = newRightGun; 
    } 
} 
1

如果用户可以找到更好的枪,一个很好的办法做到这一点是使用继承Gun类。

在ES6的例子,但你可以很容易地做到这一点在ES5采用原型:

class SimpleGun { 

    fire: function() { 
     shootLeftHandGun("handgun"); 
    } 
} 


class BetterGun extends SimpleGun { 
    fire: function() { 
     super.fire(); 
     shootRightHandGun("handgun"); 
    } 
} 

因此,当用户找到一支枪,只是做这样的事情:

user.setGun(new BetterGun()) 
+0

我不认为杰森想把这样的枪都捆绑在一起,但是否则它看起来也是个好主意。 – lauriys

+0

我同意,这取决于Jason想要达到的目标(没有足够的关于所有其他实体的信息),也取决于应用程序的大小。如果它会很大,最好考虑架构,但如果它只是一个小应用程序,你可能不需要类和其他东西。 –

+0

澄清。我需要选项,而你的答案提供了这个选项。 –

1

只要因为你的对象不是一个函数构造函数,而只是一个这样的简单对象,你可以直接向它直接添加任何你想要的东西(属性,方法):

shooter.shootLeftHandGun = fucntion() {// Your code here. } 

但是,如果您的射手是从函数构造函数创建的(这不属于您的情况),您可以通过对象的原型轻松完成此操作。

shooter.prototype.shootLeftHandGun = function() {// Your code here.} 

,而不是简单地动态创建的对象,请尝试使用此:]

function shooter() { 
    this.shootRightHandGun = function() { 
     // Code 
    } 
    this.name = "default name" 
} 

var newShooter = new shooter(); 
newShooter.prototype.shootLeftHandGun = function() { // Your new stuff.} 
+0

但是,但是,但是,如果您的shootLeftHandGun将是可重用的东西(也可以由其他对象采用,我更愿意创建一个支持所有不同类型枪支的助手对象!)这样,您可以将适当的功能注入射击对象当他们实现新的枪。无论如何,有趣的问题:) –

1

这是一个以上替代:

var shooter = { 
    guns:[{side:'right',type:'shootgun'}], 
    fire : function(){ 
     for(var i = 0; i < this.guns.length; i++){ 
      this.shoot(this.guns[i]); 
     } 
    }, 
    gainGun : function(side, type){ 
     this.guns.push({side:side, type:type}) 
    }, 
    shoot:function(gun){ 
     console.log(gun) 
    } 
    } 
1

看起来大家都在这里堆积,这很酷。这是我的尝试。如果一只手已经装备了一把枪,那么这只手就被占用了,并告知使用者。我还添加了一个fireAllGuns功能的乐趣。

var shooter = { 
    guns: {}, 
    fire: function(hand) { 
    if (this.guns[hand]) { 
     console.log("Firing " + this.guns[hand] + " from " + hand + " hand!"); 
    } else { 
     console.log("There is no gun in that hand!") 
    } 
    }, 
    fireAllGuns: function() { 
    for (var key in this.guns) { 
     if (this.guns.hasOwnProperty(key)) { 
     console.log("Firing " + this.guns[key] + " from " + key + " hand!");  
     } 
    } 
    }, 
    equipGun: function(hand, gun_name) { 
    if (!this.guns[hand]) { 
     this.guns[hand] = gun_name; 
     console.log("Equipped " + gun_name + " in " + hand + " hand"); 
    } else { 
     console.log("That hand is already occupied!") 
    } 
    } 
}; 

shooter.fire("left"); 
// There is no gun in that hand! 

shooter.equipGun("left", "pistol"); 
// Equipped pistol in left hand 

shooter.fire("left"); 
// Firing pistol from left hand! 

shooter.fire("right"); 
// There is no gun in that hand! 

shooter.equipGun("right", "bazooka"); 
// Equipped bazooka in right hand 

shooter.fire("right"); 
// Firing bazooka from right hand! 

shooter.fireAllGuns(); 
// Firing pistol from left hand! 
// Firing bazooka from right hand! 
+0

这条线做什么? (for this.guns中的var key)'。 –

+1

我正在遍历'for/in'循环。关键是手,价值就是枪的名字。检查'equipGun'函数中的赋值:'this.guns [hand] = gun_name;' –

相关问题