Dan*_*abo 10 javascript methods events object
假设我有一个带有成员函数的对象,它返回自己:
/* -- Object 1 -- */
function Object1(){
this.me = new Image(10,10);
this.me.src = "someImgUrl.jpg";
this.publish = function(){
return this.me;
}
}
Run Code Online (Sandbox Code Playgroud)
在生产中:
var Obj1 = new Object1();
document.body.appendChild( Obj1.publish() );
Run Code Online (Sandbox Code Playgroud)
现在,假设我想创建一个事件,该事件在调用对象的publish()方法时触发,但在返回图像后(类似于"onPublished()"事件).比如说,要将图像尺寸更改为100x100.我将如何创建它,以及在哪里"附加"它?
如果我不够清楚,请告诉我.这是我能想到的最简单的演示.
一个简单的例子:
function Object1() {
'use strict';
this.me = new Image(10, 10);
this.me.src = "someImgUrl.jpg";
this.publish = function() {
if (typeof this.onPublish === "function") {
setTimeout(this.onPublish, 1);
}
return this.me;
};
}
var Obj1 = new Object1();
Obj1.onPublish = function() {
// do stuff
};
Obj1.publish();
Run Code Online (Sandbox Code Playgroud)