javascript - Objects, prototypal inheritance, and a failing function -


this question has answer here:

while trying move on intermediate/advanced javascript concepts, i've run problem. in following code, object prototype , created objects seem functioning fine. when try use them in function 'winner', result declares winner second argument passed, regardless of score. here, mike has higher score(395) peter (206) , should declared winner. also, console logging message "peter wins score of function () { return (this.age * 5) + this.height; }".

any understanding i'm doing wrong appreciated.

var player = {      name: 'default',      age: 'default',      height: 'default',      score: function() {          return (this.age * 5) + this.height;      }  }        var mike = object.create(player);  mike.name = 'mike';  mike.age = 67;  mike.height = 60;      var peter = object.create(player);  peter.name = 'peter';  peter.age = 30;  peter.height = 56;      var winner = function(player1, player2) {      var player1score = player1.score;      var player2score = player2.score;            if(player1score > player2score) {          console.log(player1.name + ' wins score of ' + player1.score);      } else {          console.log(player2.name + ' wins score of ' + player2.score);      }  }    winner(mike, peter);  winner(peter, mike);

replace

var player1score = player1.score; var player2score = player2.score; 

with

var player1score = player1.score(); var player2score = player2.score(); 

you have call function, if write player1.score body of function.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -