AGS_Zero
AGS_Zero

Reputation: 13

Modifying object properties with a method

so I'm trying to write a simple game to work with objects and get used to manipulating and working with them. What I want to do is set base stats (user/programmer defined) and then use those base stats in a method to create a complex stat. sample code for what I'm trying to do:

var main = function() {
   function Boss (name, lvl, str, dex, int) {
      this.bName = name
      this.level = lvl
      this.strength = str
      this.dexterity = dex
      this.intelligence = int
      this.pAttack  = pAttack();
    }
     function pAttack() {
       (2*this.level) + (2*this.strength);
     }
      var a1Boss = new Boss("test", 50, 500, 500, 500)
      console.log(a1Boss.pAttack)
    }  

This returns undefined to the console, although everything else returns correctly. Should the pAttack function be set up as var pAttack = function() {code}

Any help would be appreciated

Upvotes: 1

Views: 82

Answers (6)

andybeli
andybeli

Reputation: 866

Lots of syntax errors.

function Boss (name, lvl, str, dex, int) {
    this.bName = name;
    this.level = lvl;
    this.strength = str;
    this.dexterity = dex;
    this.intelligence = int;
    this.pAttack  = this.pAttack();
}
Boss.prototype = {
    pAttack: function () {
        return (2*this.level) + (2*this.strength); 
    },
    pDefend: function () {
        //Defend stuff
    },
    levelUp: function () {
        this.level = this.level + 1;
    }
};

var a1Boss = new Boss("test", 50, 500, 500, 500);
console.log(a1Boss.pAttack)

https://jsfiddle.net/sLhrek8h/1/

Upvotes: 0

Paul S.
Paul S.

Reputation: 66404

Currently pAttack is not invoked in the context of a Boss instance so this is not pointing at what you expect, you have 3 options

In all cases, don't forget to return from the pAttack function!


Option 1, Have instances inherit pAttack

After defining pAttack, add the following

Boss.prototype.pAttackFn = pAttack;

What does this do?
It makes all instances of Boss, i.e. Objects constructed by new Boss, inherit the function pAttack as the property pAttackFn so you can call it e.g.

this.pAttack = this.pAttackFn();

Option 2, Define the context of invocation using .call or .apply

this.pAttack  = pAttack.call(this);

Option 3, Give pAttack parameters instead of relying on this

function pAttack(lvl, str) {
    return (2 * lvl) + (2 * str);
}

Then

this.pAttack  = pAttack(this.level, this.strength);

Upvotes: 0

Eric Dang
Eric Dang

Reputation: 21

Another solution that may help out depending on what you're doing in the long run is passing in arguments into your function, and returning that value.

function pAttack(level, strength) {
   return (2*level) + (2*strength);
}

and calling it with

this.pAttack = pAttack(this.level, this.strength); 

Upvotes: 0

Blazemonger
Blazemonger

Reputation: 92993

Your function needs to return a value. I would create it as a method of the object, however, and call it as such:

var main = function() {
    function Boss(name, lvl, str, dex, int) {
        this.bName = name;
        this.level = lvl;
        this.strength = str;
        this.dexterity = dex;
        this.intelligence = int;
        this.pAttack = function() {
            return (2 * this.level) + (2 * this.strength);
        }
    }
    var a1Boss = new Boss("test", 50, 500, 500, 500);
    console.log(a1Boss.pAttack());
};

main(); // outputs '1100' to the console

Upvotes: 0

Lewis
Lewis

Reputation: 14926

You've actually executed the method pAttack instead of assigning it. Since pAttack returns nothing, undefined is returned as an alternative.

So just don't execute it,

this.pAttack  = pAttack;

or return something

function pAttack() {
    return (2*this.level) + (2*this.strength);
}

Upvotes: 0

Mostafa EL BARRAK
Mostafa EL BARRAK

Reputation: 146

If you want your method to be attached to the new object do this :

this.pAttack = pAttack;
// and the function should be initialized as a variable
var pAttack = function(){...};

If you want your method to be attached to the prototype :

Boss.prototype.pAttack = pAttack;

Upvotes: 1

Related Questions