How to handle javascript object
This question already has an answer here:
Can not undestand whole lots of things that you have written. But the problem is ver clear. "this", you are expecting to be your method, but you have to very careful of this as it changes context based upon where you are executing it.
If I simplyfy your code, its an example of module pattern, which should be as following.
var moduleExample = (function () {
// private variables and functions
var privateVar = 'bar';
// constructor
var moduleExample = function () {
};
moduleExample.prototype.chat = function(){
alert('hello');
};
// return module
return moduleExample;
})();
var my_module = new moduleExample();
my_module.chat();
Notice how the code above has avoided the use of "this". Also notice, how a new object is created by using "new"
链接地址: http://www.djcxy.com/p/30078.html上一篇: 了解JavaScript中的原型
下一篇: 如何处理javascript对象