当我尝试在jquery mobile中使用对象方法时的代码中断
Code break when I'm trying to use an object methods in jquery mobile
这是我的类
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>
显然我
一开始使用构造函数,我忘了提到我正在使用本地存储,对象字段没问题,但是当我尝试使用这些方法时,代码会中断。
相关文章:
- 序列化数据属性中对象的最可靠方法
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 如何从对象的原型方法访问JavaScript对象属性
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- 设置嵌套对象属性的更好方法
- JQuery示例不起作用-“;对象没有't支持属性或方法'按钮'&”;
- 对象不支持属性或方法“自动完成”
- call()和apply()实际上是用来欺骗方法处理类似数组的对象的
- 对象文字方法上的Javascript绑定不起作用
- Meteor应用程序无法运行-对象#<编译器>没有方法'主机'
- 为什么我可以在Array属性对象中找到Javascript Array for Each方法
- Underscore.js某些对象的所有方法的总和
- 对象#<XMLHttpRequest>没有方法'完成'
- Es6:能够在设置/更新/删除对象属性时调用自定义方法
- 丢失对象“;这个“;方法中的上下文
- 如何在ajax中调用javascript对象的方法
- 为什么页面方法对象未定义
- 如何使用在另一个文件中定义的JavaScript方法/对象
- 如何告诉JsHint忽略“未解析的函数或方法”?对象上的警告
- 使用jinja/javascript动态创建方法/对象