Javascript中的this跟Java中的this不一样,可能在创建对象的时候是一样的,但是当运行的时候,可能javascript中的this,可能就会变成另外一个对象。如果不熟悉javascript编程的人遇到这种情况时,很有可能会觉得莫名其妙。怎么定义的函数或者属性总是undefined呢?
//@parm timeout_time if user is not active for the time, we will consider the user should be away and should make time right now.
//@parm interval_detect_timeout_time which is a timer, it will invoke "detectUserOperation" to detect whether should set onTimeout event.
function TimeoutComponent(timeout_time, interval_detect_timeout_time){
this.timeoutTime = timeout_time;
this.intervalDetectTimeoutTime = interval_detect_timeout_time;
this.detectUserOperationTimer = this.startDetect();
this.lastOperationTimestamp = new Date().getTime();
alert(this.lastOperationTimestamp);
}
TimeoutComponent.prototype.detectUserOperation = function(){
var currentTime = new Date().getTime();
var intervalTime = currentTime - this.lastOperationTimestamp;
if (intervalTime > this.timeoutTime){
//send the onTimeout event, user should override this method to customize timeout behavior
this.onTimeout();
//stop interval detect now.
this.stopDetect();
}
};
TimeoutComponent.prototype.onTimeout = function(){
};
TimeoutComponent.prototype.getTimeoutValue = function(){
return this.timeoutTime;
};
TimeoutComponent.prototype.getIntervalValue = function(){
return this.intervalDetectTimeoutTime;
};
TimeoutComponent.prototype.stopDetect = function(){
window.clearInterval(this.detectUserOperationTimer);
};
TimeoutComponent.prototype.startDetect = function(){
[b]var timer = window.setInterval(this.detectUserOperation, this.intervalDetectTimeoutTime);[/b]
return timer;
};
TimeoutComponent.prototype.updateLastOperationTime = function(){
this.lastOperationTimestamp = new Date().getTime();
};
上面是一个javascript 对象,注意上面的粗体部分。在创建这个对象时(new TimeoutComponent(60000, 30000))没有问题, 然后当运行时,我们会发现在detectUserOperation函数中的this.lastOperationTimestamp属性变成了undefined。本来这是我们TimeoutComponent对象中的一个属性,怎么现在变成undefined了呢? 当我在firefox下面调式时发现这个this已经变成了Window对象了。 难怪会找不到。 ok。问题找到了,我该怎么解决呢? ??? 想了一点时间,隐约记得以前用过dojo.lang.hitch这个方法是来处理这种情况的。ok。 改了下代码。刷新页面,ok 现在没有问题了。
TimeoutComponent.prototype.startDetect = function(){
var timer = window.setInterval(dojo.lang.hitch(this, this.detectUserOperation), this.intervalDetectTimeoutTime);
return timer;
};
看看dojo做了什么事情。。
/**
* Runs a function in a given scope (thisObject), can
* also be used to preserve scope.
*
* hitch(foo, "bar"); // runs foo.bar() in the scope of foo
* hitch(foo, myFunction); // runs myFunction in the scope of foo
*/
dojo.lang.hitch = function(thisObject, method) {
if(dojo.lang.isString(method)) {
var fcn = thisObject[method];
} else {
var fcn = method;
}
return function() {
return fcn.apply(thisObject, arguments);
}
}
ok。 现在清楚了吧。
分享到:
相关推荐
This book is for web developers and designers who work with React.js and JavaScript and who are new to unit testing and automation. It's assumed that you have a basic knowledge of JavaScript and HTML....
JavaScript中this指向.pdf
JavaScript中this指向.docx
vue 中this.$set 动态绑定数据的案例讲解 感觉网上对this.$set的讲解乱糟糟的,我来总结一下对它单个数据、对象、数组、json数据的绑定. 话不多说直接上代码: <template> <div> <!-- 单个数据 --> ...
This book will present various JavaScript and p5.js features and concepts in the following chapters. The knowledge will be reinforced by building several useful examples like an animation and a data...
this指触发事件的对象,接下来为大家分享下javascript中onclick(this)的用法,感兴趣的朋友可以参考下哈,希望对你有所帮助
JavaScript 中的 this 关键字是一个非常重要的概念,它经常会使开发者感到困 惑。通常来说,this 的值是在函数被调用时确定的,其值取决于函数被调用的方 式。本文将介绍 JavaScript 中 this 的用法,从而帮助开发者...
This Fifth Edition is completely revised and expanded to cover JavaScript as it is used in today's Web 2.0 applications. This book is both an example-driven programmer's guide and a keep-on-your-desk ...
This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object ...
Vue.set()和this.$set()使用和区别 在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vue实例后,当再次给数据赋值时,有时候并不会自动更新到视图上去; 当我们去看vue文档的时候,会发现有这么一句话...
【教程简介】 javascript基础 javascript游戏开发之贪吃蛇项目 javascript高级 nodejs开发 nosql数据之Mongodb 【javascript基础】 01-javascript的概述.md ...11-工厂方法_构造函数_this关键字.md
2.JavaScript this 3.JavaScript 闭包 4.JavaScript 事件 5.javascript 跨域 6.javascript 命名空间 Oject-Oriented 1.JavaScript Expressive 2. Interfaces 3.Introduction 4. Inheritance 5.AOP Jquery ...
You should read through this course if you want to be able to take your JavaScript skills to a new level of sophistication. Style and approach This course is a comprehensive guide where each chapter...
JavaScript程序设计
If you already know JavaScript, this book will make your transition to CoffeeScript easy. If you're new to JavaScript, it's a great place to start. New in this edition: Automating setup with the ...
If you are a JavaScript developer with some experience in development and want to increase the performance of JavaScript projects by building faster web apps, then this book is for you. You should ...
This Fifth Edition is completely revised and expanded to cover JavaScript as it is used in today's Web 2.0 applications. This book is both an example-driven programmer's guide and a keep-on-your-desk ...
JavaScript中的this指向.md
This book assumes readers understand the basics of JavaScript. What's Inside Automated development, testing, and deployment processes JavaScript fundamentals and modularity best practices Modular, ...