ehs*_*n7b 4 javascript oop bind this google-closure-library
什么是Google Closure解决thisJavaScript回调函数中关键字问题的解决方案.它在OO风格编程中非常有用.
在Google Closure中是否存在OOP的约定或样式?
更新 如何在ViewportSizeMonitor处理程序中访问this.darklayer ???
goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.math.Size');
goog.require('goog.style');
goog.require('goog.dom.ViewportSizeMonitor');
goog.provide('ehsun7b.ajax.AjaxBox');
ehsun7b.ajax.AjaxBox = function (url, containerClass) {
try {
this.url = url;
this.containerClass = containerClass;
var viwportSize = goog.dom.getViewportSize();
this.darklayer = goog.dom.createDom("div", {
"style": "width: " + viwportSize.width + "px;" + "height: " +
viwportSize.height + "px;" +
"background-image: url('css/img/50black.png');" +
"z-index: 1000;" +
"position: absolute;" +
"left: 0px; top: 0px;"
});
var vsm = new goog.dom.ViewportSizeMonitor();
goog.events.listen(vsm, goog.events.EventType.RESIZE, function(e) {
console.log("this: " + this.darklayer);
});
this.container = goog.dom.createDom("div", {
"class": this.containerClass
});
goog.dom.appendChild(this.darklayer, this.container);
goog.dom.setTextContent(this.container, "hello ajax box");
this.show = function() {
goog.dom.appendChild(document.body, this.darklayer);
},
this.hide = function() {
goog.dom.removeNode(this.darklayer);
}
} catch (e) {
console.log("error: " + e);
}
};
Run Code Online (Sandbox Code Playgroud)
我按照Closure的风格改变了我的课程:
goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.math.Size');
goog.require('goog.style');
goog.require('goog.dom.ViewportSizeMonitor');
goog.provide('ehsun7b.ajax.AjaxBox');
ehsun7b.ajax.AjaxBox = function (url, containerClass) {
try {
this.url = url;
this.containerClass = containerClass;
var viwportSize = goog.dom.getViewportSize();
this.darklayer = goog.dom.createDom("div", {
"style": "width: " + viwportSize.width + "px;" + "height: " +
viwportSize.height + "px;" +
"background-image: url('css/img/50black.png');" +
"z-index: 1000;" +
"position: absolute;" +
"left: 0px; top: 0px;"
});
var vsm = new goog.dom.ViewportSizeMonitor();
goog.events.listen(vsm, goog.events.EventType.RESIZE, function(e) {
console.log("this: " + this.darklayer);
});
this.container = goog.dom.createDom("div", {
"class": this.containerClass
});
goog.dom.appendChild(this.darklayer, this.container);
goog.dom.setTextContent(this.container, "hello ajax box");
} catch (e) {
console.log("error: " + e);
}
};
ehsun7b.ajax.AjaxBox.prototype.show = function() {
goog.dom.appendChild(document.body, this.darklayer);
}
ehsun7b.ajax.AjaxBox.prototype.hide = function() {
goog.dom.removeNode(this.darklayer);
}
Run Code Online (Sandbox Code Playgroud)
goog.bind是通用解决方案.例如:
goog.bind(this.someFunction, this);
goog.bind(this.someFunction, this, arg1);
goog.bind(this.someFunction, this, arg1, arg2);
Run Code Online (Sandbox Code Playgroud)
框架通常设计为可以避免这种情况,因此不必显式调用goog.bind.
例如,goog.events.EventHandler自动将回调绑定到您在其构造函数中设置的处理程序.
var handler = new goog.events.EventHandler(this);
handler.listen(something, 'something', this.someFunction); // no need to bind
Run Code Online (Sandbox Code Playgroud)
数组函数还支持处理程序参数.
goog.array.forEach(elements, this.someFunction, this);
var items = goog.array.map(elements, this.someFunction, this);
Run Code Online (Sandbox Code Playgroud)
等等.框架的大多数部分都很容易做到这一点,它非常适合"伪经典"继承.
有关更多详细信息,请参阅http://www.bolinfest.com/javascript/inheritance.php