将原型分配给事件 [英] assign prototype to event

查看:80
本文介绍了将原型分配给事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想知道为什么以下工作,在IE6上,但有一个错误:Not

已实施。


函数TEST(){}

TEST.prototype.Initialize = function()

{

var mImage = new Image();

var mDate = new Date();

var start = mDate.getTime();

mImage。 onload = this.Alerting(start); // WORKS with ERROR" Not implemented"

//mImage.onload = function(){this.Alerting(start);} // ERROR"对象没有

支持这个属性或方法

mImage.src =" winxp.gif" ;;

}

TEST.prototype.Alerting = function(i_string){alert(i_string);}

var mTest = new TEST();

mTest.Initialize( );


有谁可以猜到为什么?

感谢您的时间。

Hello,

I am wondering why the following works, on IE6, but with an error : "Not
implemented".

function TEST(){}
TEST.prototype.Initialize = function()
{
var mImage = new Image();
var mDate = new Date();
var start = mDate.getTime();
mImage.onload = this.Alerting(start);//WORKS with ERROR "Not implemented"
//mImage.onload = function(){this.Alerting(start);}//ERROR "Object doesn''t
support this property or method"
mImage.src = "winxp.gif";
}
TEST.prototype.Alerting = function(i_string){alert(i_string);}
var mTest = new TEST();
mTest.Initialize();

Can anybody guess why?
Thanks for your time.

推荐答案

mImage.onload = this.Alerting(start); // WORKS with ERROR" Not implemented&quo t;


这个语法对于你要做的事情是不正确的。你想要

mImage.onload被设置为一个函数 - 你将它设置为

从该函数调用返回。

创建一个匿名函数,调用Alerting(..)


var _this = this; //重要的,否则你将失去对匿名函数中的这个TEST实例和this的引用

将参考

图像对象(我认为)。


mImage.onload = function(start){_ this.Alerting(start);}

mImage.onload = this.Alerting(start);//WORKS with ERROR "Not implemented"

this syntax is incorrect for what you''re trying to do. you want
mImage.onload to be set to a function- you''re setting it to whatever
is returned from that function call.
create an anonymous function that calls "Alerting(..)"

var _this = this; //important otherwise you will lose reference to
this TEST instance in the anonymous function and "this" will refer to
the image object (i think).

mImage.onload = function(start){_this.Alerting(start);}


webgour写道:
webgour wrote:

我想知道为什么以下工作,在IE6上,但与

错误:未实施。
I am wondering why the following works, on IE6, but with
an error : "Not implemented".



声明不工作的原因之一在这里展示时,并没有受到

好​​评,没有人对

声明留下深刻印象它的工作原理是那些说工作,在IE6上,但是

但有错误的人。

One of the reasons that the declaration "don''t work" is not greeted with
praise when presented here, and nobody is too impressed with the
declaration "it works" is people who say things like "works, on IE6, but
with an error".


function TEST(){}

TEST.prototype.Initialize = function()

{

var mImage = new Image();

var mDate = new Date();

var start = mDate.getTime();

mImage.onload = this.Alerting(start); // WORKS with ERROR"不是

实施的
function TEST(){}
TEST.prototype.Initialize = function()
{
var mImage = new Image();
var mDate = new Date();
var start = mDate.getTime();
mImage.onload = this.Alerting(start);//WORKS with ERROR "Not
implemented"



此对象的 - Alerting - 方法没有返回语句

因此默认返回未定义的值。 IE反对将未定义的值分配给内部事件处理程序的想法。其中

不太合理,因为它允许在清除

处理程序时分配空值,否则尝试
是没有意义的
将除函数引用之外的任何内容分配给 - onload - 属性。

The - Alerting - method of this object does not have a return statement
and so returns the undefined value by default. IE is objecting to the
idea of assigning an undefined value to an intrinsic event handler. Which
is not too unreasonable as it allows the assigning of a null value when a
handler is to be cleared and otherwise it makes no sense to attempt to
assign anything but a reference to a function to the - onload - property.


//mImage.onload = function(){this.Alerting(start) );}}错误

对象不支持此属性或方法
//mImage.onload = function(){this.Alerting(start);}//ERROR
"Object doesn''t support this property or method"





响应事件中触发的内在事件处理方法的上下文中, - this - keyword将(或将,名为

的DOM(通常与Image对象一起使用)引用已分配

函数的对象,在本例中为Image对象。 Image

对象没有Alerting方法,因此IE告诉你它不支持名为

的方法。

In the context of an intrinsic event handling method triggered in
response to an event the - this - keyword will (or, will with DOM named
and usually will with Image objects) refer to the object to which the
function has been assigned, the Image object in this case. The Image
object has no Alerting method so IE tells you it does not support the
method called.


mImage.src =" winxp.gif";

}

TEST.prototype.Alerting = function(i_string){alert(i_string);}

var mTest = new TEST();

mTest.Initialize();


有谁可以猜到为什么?
mImage.src = "winxp.gif";
}
TEST.prototype.Alerting = function(i_string){alert(i_string);}
var mTest = new TEST();
mTest.Initialize();

Can anybody guess why?



< snip>


您还想猜猜吗?关于VK的问题:操作系统特定的gc

失败,并在window.frames集合中引用

引用。


Richard。

<snip>

You want a guess as well? How about on of VK''s: "an OS-specific gc
failure with
references in window.frames collection".

Richard.


3月12日下午4:18,Richard Cornford < Rich ... @ litotes.demon.co.uk>

写道:
On Mar 12, 4:18 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

webgour写道:
webgour wrote:

我想知道为什么以下工作,在IE6上,但是

错误:未实现。
I am wondering why the following works, on IE6, but with
an error : "Not implemented".



声明不工作的原因之一在这里展示时,并没有受到

好​​评,没有人对

声明留下深刻印象它的工作原理是那些说工作,在IE6上,但是

但有错误的人。


One of the reasons that the declaration "don''t work" is not greeted with
praise when presented here, and nobody is too impressed with the
declaration "it works" is people who say things like "works, on IE6, but
with an error".


function TEST(){}

TEST.prototype.Initialize = function()

{

var mImage = new Image();

var mDate = new Date();

var start = mDate.getTime();

mImage.onload = this.Alerting(start); // WORKS with ERROR"不是

实施的
function TEST(){}
TEST.prototype.Initialize = function()
{
var mImage = new Image();
var mDate = new Date();
var start = mDate.getTime();
mImage.onload = this.Alerting(start);//WORKS with ERROR "Not
implemented"



此对象的 - Alerting - 方法没有返回语句

因此默认返回未定义的值。 IE反对将未定义的值分配给内部事件处理程序的想法。其中

不太合理,因为它允许在清除

处理程序时分配空值,否则尝试
是没有意义的
将除函数引用之外的任何内容分配给 - onload - 属性。


The - Alerting - method of this object does not have a return statement
and so returns the undefined value by default. IE is objecting to the
idea of assigning an undefined value to an intrinsic event handler. Which
is not too unreasonable as it allows the assigning of a null value when a
handler is to be cleared and otherwise it makes no sense to attempt to
assign anything but a reference to a function to the - onload - property.


//mImage.onload = function(){this.Alerting(start) );}}错误

对象不支持此属性或方法
//mImage.onload = function(){this.Alerting(start);}//ERROR
"Object doesn''t support this property or method"



在一个内部事件处理方法的上下文中,

响应一个事件 - 这个 - 关键字将会(或将,名为

的DOM(通常与Image对象一起使用)引用已分配

函数的对象,在本例中为Image对象。 Image

对象没有Alerting方法,因此IE告诉你它不支持名为

的方法。


In the context of an intrinsic event handling method triggered in
response to an event the - this - keyword will (or, will with DOM named
and usually will with Image objects) refer to the object to which the
function has been assigned, the Image object in this case. The Image
object has no Alerting method so IE tells you it does not support the
method called.


mImage.src =" winxp.gif";

}

TEST.prototype.Alerting = function(i_string){alert(i_string);}

var mTest = new TEST();

mTest.Initialize();
mImage.src = "winxp.gif";
}
TEST.prototype.Alerting = function(i_string){alert(i_string);}
var mTest = new TEST();
mTest.Initialize();


有谁可以猜到为什么?
Can anybody guess why?



< snip>


您还想猜猜吗?关于VK的问题:操作系统特定的gc

失败,并在window.frames集合中引用

引用。


理查德。


<snip>

You want a guess as well? How about on of VK''s: "an OS-specific gc
failure with
references in window.frames collection".

Richard.



在javascript中为预定义的

对象分配自定义事件处理程序时,我会非常小心。不同的浏览器以非常不同的方式处理那种

分配。考虑:


函数TEST {

var mImage = new Image();

mImage.onload = TEST.prototype.something ;

mImage.src =" whatever" ;

}

TEST.prototype.something = function(){

alert(this);

}


您希望每个浏览器都提醒对象图像或[对象图片]

或其中的一些变体,但在Safari中你会发现它返回

" object Window"。br />

一个月前遇到类似的问题:
http://groups.google.com/group/comp....rosera&rnum=1#

I''d be real careful when assigning custom event handlers to predefined
objects in javascript. Different browsers handle that kind of
assignment very differently. Consider:

function TEST {
var mImage = new Image() ;
mImage.onload = TEST.prototype.something;
mImage.src = "whatever" ;
}
TEST.prototype.something = function() {
alert(this) ;
}

You''d expect every browser to alert "object Image" or "[object Image]"
or some variation on that, but in Safari you''ll find that it returns
"object Window".

Had a similar problem a month ago:
http://groups.google.com/group/comp....rosera&rnum=1#


这篇关于将原型分配给事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆