当我尝试在jquery mobile中使用对象方法时的代码中断

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

本文关键字:方法 对象 中断 代码 mobile jquery      更新时间:2023-09-26

这是我的类

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 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>

显然我

一开始使用构造函数,我忘了提到我正在使用本地存储,对象字段没问题,但是当我尝试使用这些方法时,代码会中断。