0

I am trying to make a IF statement that basically says this...

"If 'character' is a prototype of 'player' then...", but i'm not quite sure how to write this in code. If the character IS, then it will go with the player route. If it is not, then it will detect it as a monster and operate the opposite function instead.

var actor = {   
expCounter: function (character){
        if (player){ // This is what I want to change
            $("#playerexpbaroverlay").animate({
                height: dwarf.exp
            }, 200);
        }
        else if (monster){ //if character is not a prototype of player, do this
            $("#bossexpbaroverlay").animate({
                height: dwarf.exp
            }, 200);
        }
        if (dwarf.exp > 199){
            dwarf.level++
            dwarf.exp = 1;
            dom.setText("playerlevel", dwarf.level)

            $("#playerexpbaroverlay").animate({
            height: dwarf.exp
            }, 100);
        }
     }

}

Extra code I have:

New = Object.create;

player = New (actor),
monster = New (actor),

dwarf = New(player),
angel = New(monster);
Shawn
  • 1,175
  • 2
  • 11
  • 20

5 Answers5

1

Yet another way, just add flag, like isPlayer to player object, like

var actor = {};
New = Object.create;

var player = New(actor),
  monster = New(actor),

  dwarf = New(player),
  angel = New(monster);

player.isPlayer = true;


function Check(character) {
  if (character.isPlayer) {
    console.log('player');
  } else {
    console.log('not player');
  }
}
Check(dwarf);
Check(angel);
Grundy
  • 13,356
  • 3
  • 35
  • 55
  • or set type like....player.setType("player"), player.setType("monster") and then check – Harpreet Singh Dec 09 '15 at 08:54
  • @HarpreetSingh, _player_ not link with _monster_, so i not quite understand what you mean in comment with _setType_ :-) – Grundy Dec 09 '15 at 08:56
  • its a var...it could be anything.. X.setType like – Harpreet Singh Dec 09 '15 at 08:58
  • 1
    Actor is linked with player object & monster object. Player object is linked to dwarf and Monster object is linked to Angel. I will be adding future players & monsters in the future, so being able to get this IF statement right the first time around, would save a lot of headache in the future. I don't see how setting a type will help, other than add redundant code. – Shawn Dec 09 '15 at 08:59
  • If you need a perfect answer, try to explain it in better way. @user2763154 – Harpreet Singh Dec 09 '15 at 09:00
  • @louisbros, why on each instance? just on player in this case, all derived object, get this flag from prototype – Grundy Dec 09 '15 at 09:38
  • @Grundy yes you're right, but still isn't this a replicating `instanceof`? It just seems like an extra convention to maintain. – louisbros Dec 09 '15 at 09:40
  • @louisbros, in this case _instanceof_ not applicable, because now OP have not constructors. So this simplest variant to check this. – Grundy Dec 09 '15 at 09:43
0

have you tried isPrototypeOf() ? The isPrototypeOf() method tests for an object in another object's prototype chain.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Simply use instanceof method like this

if(player instanceof Player){ //player is your actual function / class you created instance from

}else if(monster instanceof Monster){
   // do something
}
Harry Bomrah
  • 1,658
  • 1
  • 11
  • 14
  • this not work in this case, you should just try it. OP have not _Player_ class, just _player_ object – Grundy Dec 09 '15 at 08:47
0

You can use instanceof to check whether an object has the prototype of another it it's prototype chain.

The Object.create() function can be used to inherit the prototype of another 'class':

var New = Object.create;

function Actor() {}

// Player > Actor
function Player() {}
Player.prototype = New(Actor.prototype);

// Monster > Actor
function Monster() {}
Monster.prototype = New(Actor.prototype);

// Dwarf > Player > Actor
function Dwarf() {}
Dwarf.prototype = New(Player.prototype);

// Angel > Monster > Actor
function Angel() {}
Angel.prototype = New(Monster.prototype);

// create the instance
var angel = new Angel();

console.log(angel instanceof Player); // false
console.log(angel instanceof Monster); // true
console.log(angel instanceof Actor); // true

Demo: https://jsfiddle.net/louisbros/j8r4qqjc/

louisbros
  • 865
  • 5
  • 10
0

Assuming that you have a Javascript Class that defines the Object of type player, or something else... you can do something like that:

function Monster(name) {
  this.name = name;
}

function Player(name) {
  this.name = name;
}

var hitmands = new Player('hitmands');
var sagat = new Monster('Sagat');

console.log('is hitmands a monster?', hitmands instanceof Monster);
console.log('is hitmands a Player?', hitmands instanceof Player);

A good way to do that is to assign a static method to each class that checks if a given instance belongs to a determinate prototype chain:

function Monster(name) {
  this.name = name;
}

function Player(name) {
  this.name = name;
}

Player.is = function(data) {
  return data instanceof Player;
}

Monster.is = function(data) {
  return data instanceof Monster;
}

var hitmands = new Player('hitmands');
var sagat = new Monster('Sagat');

console.log('is hitmands a monster?', Monster.is(hitmands) );
console.log('is hitmands a Player?', Player.is(hitmands) );

Unfortunately javascript doesn't provide any way to implement a Operator overloading

further reading:

Javascript operator overloading

Javascript instanceof

Community
  • 1
  • 1
Hitmands
  • 13,491
  • 4
  • 34
  • 69