简体   繁体   English

JavaScript - 使用数组符号将 function 添加到原型

[英]JavaScript - Add function to prototype using array notatio

Is it possible to do something like that?有可能做这样的事情吗?

Foo.add_function = function (callback_name, callback_function) {
    this.prototype[callback_name] = callback_function;
}

I need to add a function to prototype in array notation.我需要在数组符号中添加一个 function 到原型。 Thank you.谢谢你。

Yes.是的。
A prototype is a normal object; prototype是普通的object; that will work fine.那会很好用。

Note that this will only work if this is a function;请注意, this仅适用于 function; otherwise, prototype won't exist.否则, prototype将不存在。

Apparently, yes:显然,是的:

http://jsfiddle.net/QUFXU/6/ http://jsfiddle.net/QUFXU/6/

jsfiddle is great, you should check it out when you want to test out simple stuff like this. jsfiddle 很棒,当你想测试像这样的简单东西时,你应该检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM