我正在嘗試爲我製作的一個小畫布庫製作事件類!如何在JavaScript中創建事件基類?
這是我到目前爲止,
function Events() {
};
Events.prototype.addEvents = function() {
this.mousemove = false;
this.onMousemove = function() {
if (this.mousemove) {
this.mousemove();
}
};
this.mousedown = false;
this.onMousedown = function() {
if (this.mousedown) {
this.mousedown();
}
};
this.mouseup = false;
this.onMouseup = function() {
if(this.mouseup) {
this.mouseup();
}
};
this.click = false;
this.onClick = function() {
if (this.click) {
this.click();
}
};
this.on = function(type, callback) {
};
};
什麼我不能做的就是現在這個添加到我想要分配上的事件的其他對象。例如,另一個對象可能是一個簡單的具有繪製方法的矩形。
function Rect() {
this.draw = function(context) {
// code
};
};
// How can I add Events prototype properties to Rect? I have tried...
// Rect.prototype = new Object.create(Events.prototype);
// Rect.prototype = Object.create(new Events.prototype);
我基本上想要事件原型將其所有的屬性給予我想要發生事件的另一個對象?
謝謝,我希望我有道理!
編輯澄清一點點更多的解釋。我第一次打得太快,並錯誤地解釋了臨時構造函數F()的好處。 – 2013-05-04 13:14:25