Rexford Brittingham
Rexford Brittingham

Reputation: 15

Object defined as object returns undefined

I am designating player as an object and within it are properties such as weapon which are objects I have defined as an item such as stick which will have a value attached to it that it could be damage. However, when called on by a function player.weapon.stick it will return the error "Uncaught TypeError: Cannot read property 'stick' of undefined". I believe it is saying weapon is undefined but the block I have defining all of this is

//Player Data
var player = {
weapon: stick,
speed: 3,
armor: cloth,
location: pLocation
}
var pLocation = [tickX, tickY];

//Items
var stick = { stick: 1 };
var cloth = { ClothArmor: 1 };

Is the issue within my defining of it or how I'm calling it player.weapon.stick

Upvotes: 1

Views: 87

Answers (4)

Charlie
Charlie

Reputation: 23768

JS only hoist functions - not variables. So, you have to define variables prior using them.

var pLocation = [tickX, tickY];

    //Items
    var stick = { stick: 1 };
    var cloth = { ClothArmor: 1 };

    //Player Data
    var player = {
    weapon: stick,
    speed: 3,
    armor: cloth,
    location: pLocation
    }

or

var stick

//Player Data
var player = {
weapon: stick,
speed: 3,
armor: cloth,
location: pLocation
}
var pLocation = [tickX, tickY];

//Items
stick = { stick: 1 };
var cloth = { ClothArmor: 1 };

Upvotes: 1

Simon Hyll
Simon Hyll

Reputation: 3608

You have several issues here, the biggest one being that you don't seem to understand objects (not trying to be rude, but you're using them waaay wrong).

var pLocation = [tickX, tickY]; // place this first to avoid undefined location
var player = {
weapon: stick, // stick without quotation marks is a variable, not a string, so this would be player.weapon = stick where stick = undefined variable.
speed: 3,
armor: cloth,
location: pLocation // since pLocation is defined after in your script i currently has no value, so location will be undefined.
}
//alternatively for pLocation
player.location = [tickX, tickY];

//Items
var stick = { stick: 1 }; // this creates an object called stick with a variable called stick with the value 1, so stick.stick = 1.
var cloth = { ClothArmor: 1 }; // this creates an object called cloth with a variable called ClothArmor with the value 1, so cloth.ClothArmor = 1.

So if you call player.weapon.stick it's incorrect because weapon is not an object, it's a variable inside an object. If you want player.weapon.stick you have to use:

player = {
    weapon: {
        stick: 'Variable value'
    }
}

Upvotes: 0

JoshKisb
JoshKisb

Reputation: 752

It is best to define variables before using them

var pLocation = [tickX, tickY];

//Items
var stick = { stick: 1 };
var cloth = { ClothArmor: 1 };

//Player Data
var player = {
    weapon: stick,
    speed: 3,
    armor: cloth,
    location: pLocation
}

Upvotes: 1

Nir Alfasi
Nir Alfasi

Reputation: 53525

cloth and stick should be defined when you declare your player, since they're not defined yet their value is resolved to undefined and it won't change later on when you define them.

Upvotes: 1

Related Questions