Javascript this关键字返回undefined
Javascript this keyword return undefined
我正在尝试用OOP文字方式构建幻灯片放映功能。
所以,这是我的代码:
"use strict";
var slideshow = {
elSet : $(".slideshow"),
elCount : indexCount(".dealList"),
elWidth : width(".dealList"),
elNo : 1,
next : function() {
if (this.elNo < this.elCount) {
console.log(this.elSet);
this.elNo += 1;
this.elSet.style.transform = "translateX(-" + this.elWidth * this.elNo + "px)";
}
else {
console.log(this.elSet);
this.elNo = 1;
this.elSet.style.transform = "translateX(-" + this.elWidth * this.elNo + "px)";
}
},
initial : function() {
var loop = setInterval(this.next, 5000);
}
}
slideshow.initial();
浏览器控制台出现问题:
- 内存不足
- console.log返回未定义
问题可能是因为这个关键词而发生的吗?
我的代码出了什么问题?
setInterval()
的回调绑定到全局对象,而不是您的对象。但是,您可以使用以下代码将其绑定到您的对象:
initial : function() {
var loop = setInterval(this.next.bind( this ), 5000);
}
bind()
上的MDN
相关文章:
- RequireJS向模块传递配置总是返回undefined
- JSON数组数据返回Undefined
- JavaScript”;.方法“;返回undefined
- jQuery可以't获取data-*属性值,返回undefined
- Javascript/JSON警报返回Undefined
- Javascript:firstChild对象返回undefined
- 使用在函数外部声明的变量的 Javascript 返回值 + undefined
- 当尝试使用ajax读取文件时,变量返回undefined
- 如何避免JSON.stringify在特殊情况下返回undefined,从而为JSON.parse创建字符串失败
- 使用谷歌地图:一个返回undefined的自定义javascript函数
- Json在nodeJs和AngularJs中返回undefined
- 为什么像 Firefox 和 Chrome 控制台中的 '}{' 这样的结构会返回 'undefined'?
- 使用 HTML 和 JavaScript 从表单访问值时返回 undefined
- 在 CoffeeScript 中,当尝试调用类中的方法 (@_methodName) 时,它会返回 undefined
- 解析承诺'当'返回undefined(Javascript)
- NodeJS:zlib.gunzip(body)错误地返回“undefined”
- window.open在chrome扩展中返回undefined
- javascript中的If/else不会为undefined返回else
- “undefined"返回而不是游戏名称
- 为什么“typeof false || undefined”返回"布尔值”;