javascript - Code break when I'm trying to use an object methods in jquery mobile -


this class

var player = function (name) {          this.init(name);      }      $.extend(player.prototype, {          name: '',         goals: 0,         fouls: 0,         holding: 0,         games: 0,         wins: 0,        taken: 0,          init: function(name){             this.name = name;            this.goals= 0;            this.fouls= 0;            this.holding= 0;            this.games= 0;            this.wins= 0;            this.taken= 0;         },         setgoal: function (num) {             this.goals+= num;          },         setfouls: function (num) {             this.fouls+=num;         },         setholding: function (holding) {             this.holding = (this.holding * (this.games-1) + holding) / (this.games);         },         setgames: function () {              this.games+=1;         },         setwins: function () {              this.wins+=1;         },         settaken: function (num) {              this.taken+=num;         }  }); 

i tryed many thing evry time i'm trying access method after creating instance of class break , dont let me continues.

i not see using constructor being used.

it should used way:

var player = function(name) {    this.init(name);    }    $.extend(player.prototype, {      name: '',    goals: 0,    fouls: 0,    holding: 0,    games: 0,    wins: 0,    taken: 0,        init: function(name) {        this.name = name;      this.goals = 0;      this.fouls = 0;      this.holding = 0;      this.games = 0;      this.wins = 0;      this.taken = 0;      },    setgoal: function(num) {      this.goals += num;      },    setfouls: function(num) {      this.fouls += num;    },    setholding: function(holding) {      this.holding = (this.holding * (this.games - 1) + holding) / (this.games);    },    setgames: function() {        this.games += 1;    },    setwins: function() {        this.wins += 1;    },    settaken: function(num) {        this.taken += num;    }  });  var p = new player('rayon');  p.setgoal(10);  alert(p.name +' has scored '+p.goals +' goals');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -