【问题标题】:Code break when I'm trying to use an object methods in jquery mobile当我尝试在 jquery mobile 中使用对象方法时出现代码中断
【发布时间】:2023-03-25 04:54:01
【问题描述】:

这是我的课

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;
        }
 });

我尝试了很多东西,但是每次我在创建此类的实例后尝试访问一个方法时,它都会中断并且不要让我继续。

【问题讨论】:

    标签: javascript jquery class object jquery-mobile


    【解决方案1】:

    我没有看到你在使用正在使用的构造函数。

    应该这样使用:

    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>

    【讨论】:

    • 显然我一开始使用构造函数,对象文件没问题,但是当我尝试在函数中使用对象方法时,代码中断了。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多