javascript 对象没有“应用”方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11666301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:57:08  来源:igfitidea点击:

Object has no method 'apply'

javascriptjquery

提问by AdityaParab

I am creating a few DOM elements dynamically like,

我正在动态创建一些 DOM 元素,例如,

var anchorElement = jQuery('<a />',{text:property.text});
var liElement = jQuery('<li />',{"class":"navlink_"+i,id:"navlink_"+i});
anchorElement.on('click',property.fnctn);
liElement.append(anchorElement);
parentID.append(liElement);

Where propertyis a JSON object. property.textis the text that I want to put into anchor element. (Works fine)

propertyJSON 对象在哪里。 property.text是我想放入锚元素的文本。(工作正常)

I want to attach a click event handler to that anchor element. The function that needs to be bound to that element is specified in JSON and we can access it like

我想将点击事件处理程序附加到该锚点元素。需要绑定到该元素的函数在 JSON 中指定,我们可以像这样访问它

property.fnctn

The following line should bind the event handler to the anchor element.

以下行应将事件处理程序绑定到锚点元素。

anchorElement.on('click',property.fnctn);

This was not working so I tried converting it into string like,

这不起作用,所以我尝试将其转换为字符串,例如,

anchorElement.on('click',property.fnctn.toString());

No Success...

没有成功...

When I click on this link, the error is logged in the console

当我单击此链接时,错误会记录在控制台中

The object has no method 'apply'. What is the reason...???

该对象没有“应用”方法。是什么原因...???

I am able to get it working with a slight work around like

我可以通过一些轻微的工作来让它工作,比如

anchorElement.attr('onclick',property.fnctn+"()");

Above statement works, but I want to know why .on()API is not working.

上面的语句有效,但我想知道为什么.on()API 不起作用。

Thanks :) ADitya.

谢谢:) ADitya。

回答by T.J. Crowder

Update:

更新

Youve said that property.actfnis a string, "paySomeoneClick". It's best not to use strings for event handlers, use functionsinstead. If you want the function paySomeoneClick, defined in the string, to be called, and if that function is global, you can do this:

你说过这property.actfn是一个字符串,"paySomeoneClick". 最好不要将字符串用于事件处理程序,而是使用函数。如果您希望paySomeoneClick在字符串中定义的 function被调用,并且该函数是全局的,则可以执行以下操作:

anchorElement.on('click',function(event) {
    return window[property.fnctn](event);
});

That works because global functions are properties of the global object, which is available via windowon browsers, and because of the bracketed notation described below.

这是有效的,因为全局函数是全局对象的属性,可通过window浏览器获得,并且因为下面描述的括号表示法。

If the function is on an object you have a reference to, then:

如果该函数位于您引用的对象上,则:

anchorElement.on('click',function(event) {
    return theObject[property.fnctn](event);
});

That works because in JavaScript, you can access properties of objects in two ways: Dotted notation with a literal property name (foo.baraccesses the barpropety on foo) and bracketed notation with a string property name (foo["bar"]). They're equivalent, except of course in the bracketed notation, the string can be the result of an expression, including coming from a property value like property.fnctn.

这是有效的,因为在 JavaScript 中,您可以通过两种方式访问​​对象的属性:带有文字属性名称的点符号(foo.bar访问bar属性 on foo)和带有字符串属性名称(foo["bar"])的方括号符号。它们是等价的,当然除了括号中的符号外,字符串可以是表达式的结果,包括来自像property.fnctn.

But I would recommend stepping back and refactoring a bit so you're not passing function names around in strings. Sometimesit's the right answer, but in my experience, not often. :-)

但我建议退后一步并稍微重构一下,这样您就不会在字符串中传递函数名称。有时这是正确的答案,但根据我的经验,并不经常。:-)

Original answer:

原答案

(This assumed that property.fnctnwas a function, not a string. But may be of some use to someone...)

(这假设它property.fnctn是一个函数,而不是一个字符串。但可能对某人有用......)

The code

代码

anchorElement.on('click',property.fnctn);

will attach the function to the event, but during the call to the function, thiswill refer to the DOM element, notto your propertyobject.

将函数附加到事件,但在调用函数期间,this将引用 DOM 元素,而不是您的property对象。

To get around that, use jQuery's $.proxy:

要解决这个问题,请使用 jQuery 的$.proxy

anchorElement.on('click',$.proxy(property.fnctn, property));

...or ES5's Function#bind:

...或 ES5 的Function#bind

anchorElement.on('click',property.fnctn.bind(property));

...or a closure:

...或关闭:

anchorElement.on('click',function(event) {
    return property.fnctn(event);
});

More reading (on my blog):

更多阅读(在我的博客上):