Javascript, pass-by-value an object to a constructor -


i'm developing game using node.js ; have bot class containing position attribute , fire method. method acts follow:

bot.prototype.fire = function () {     this.bombs.push(new bomb(this.position)); }; 

obviously, bomb's position has differ bot's position, want this.position copied new bomb object. in case, object passed constructor reference , not value, if write this.position = position in bomb's constructor, game not work.

now question pretty straightforward : there simple way (i don't want define new function that) pass position object value instead of reference? or forced write in bomb's constructor :

this.position = {     x: position.x,     y: position.y,     angle: position.angle }; 

thanks in advance :)

you can't pass object reference. can clone object before pass it. simplest use object.create():

bot.prototype.fire = function () {     this.bombs.push(new bomb(object.create(this.position))); }; 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -