Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

brandonlind2
brandonlind2
7,823 Points

how can I pass object properties, to in a argument of a object created with a constructor function?

I'm attempting to a create a simple text based video game as practice project. The last line of code isnt working, how could I modify this code to get it to work? sorry i cant get the code to highlight correctly

//ammo
function Ammo(damage,name){
    this.damage=damage;
    this.name=name;} 
var ammo={
    556: new Ammo(30,'5.56'),
    308: new Ammo(80,'.308'),
    ninemm: new Ammo(30,'9mm'),
    45: new Ammo(80,'45acp'),
    762: new Ammo(50,'7.62x39'),
    22: new Ammo(10,'.22')}
//weapons
function RangedWeapon(accuracy,damage,speed,ammo){
    this.accuracy=accuracy;
    this.damage=damage;
    this.speed=speed;
    this.attachments= []; 
        this.upgrades= [];}
RangedWeapon.prototype.attachmentAdd= function(attachment){ 
        this.attachments.push(attachment);}
RangedWeapon.prototype.upgrades= function(upgrade){
    this.upgrades.push(upgrade);}
var RangedWeapons= {
    ar15: new RangedWeapon(80,ammo.556.damage,50,ammo.556)
        }; 

2 Answers

Julie Myers
Julie Myers
7,627 Points

I messed around with you code and what I found is the only key I could get to in the ammo object is ninemm. I got the following error when I tried to reach the keys that start with a number: "Uncaught SyntaxError: Unexpected number(…)"

If you look at javaScript variable rules they can not start with a number. That rules seems to apply for object key names as well. If you change all the ammo object key's to start with a letter that should solve your issue.

This is how I redid your coding:

function Ammo(damage,name){
    this.damage=damage;
    this.name=name;
}

var ammo={
    five56: new Ammo(30,'5.56'),
    three08: new Ammo(80,'.308'),
    ninemm: new Ammo(30,'9mm'),
    four5: new Ammo(80,'45acp'),
    seven62: new Ammo(50,'7.62x39'),
    twenty2: new Ammo(10,'.22')
}

//weapons
function RangedWeapon(accuracy,damage,speed,ammo){
    this.accuracy=accuracy;
    this.damage=damage;
    this.speed=speed;
    this.attachments= []; 
        this.upgrades= [];
}

RangedWeapon.prototype.attachmentAdd= function(attachment){ 
        this.attachments.push(attachment);
}

RangedWeapon.prototype.upgrades= function(upgrade){
    this.upgrades.push(upgrade);
}

var RangedWeapons= {
    ar15: new RangedWeapon(80,ammo.five56.damage,50,ammo.five56)
}; 

By the way I hadn't thought of calling a constructor function from inside of an object.

brandonlind2
brandonlind2
7,823 Points

thanks, I figured it would make it drier because var isn't being repeated for every object created, I didn't think about the names starting with a number, I fixed that and its working now thanks