[英]Adding a javascript function to an already existing javascript object
I wrote a javascript function for Telerik RadTreeView as follows: 我为Telerik RadTreeView编写了一个javascript函数,如下所示:
function CheckedNodeValues() {
var checkedNodes = $(this).get_checkedNodes();
for (var n in checkedNodes) {
checkedNodes[n].get_value();
}
}
I would like to be able to do myRadTreeView.CheckedNodeValues()
as opposed to CheckedNodeValues(myRadTreeView);
我想能够做
myRadTreeView.CheckedNodeValues()
而不是CheckedNodeValues(myRadTreeView);
Is there a way to add a function to a class in Javascript? 有没有办法在Javascript中向类添加函数? I hope what I'm asking is clear, ideally, I would like extend the methods available on a class.
我希望我要清楚的是,理想情况下,我想扩展类中可用的方法。
You should extend the prototype of your object, like this: 您应该扩展对象的原型,如下所示:
myRadTreeView.prototype.CheckedNodeValues = function () {
// your code here
};
You can read more about the prototype concept here: http://www.javascriptkit.com/javatutors/proto.shtml or here: http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/ 您可以在以下位置阅读有关原型概念的更多信息: http : //www.javascriptkit.com/javatutors/proto.shtml或此处: http : //javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/
Absolutely: 绝对:
myRadTreeView.CheckedNodeValues = function () {
// do totally rad stuff here
};
To call it you would use: 调用它可以使用:
myRadTreeView.CheckedNodeValues();
Something like this should do .. 这样的事情应该做..
Telerik.Web.UI.RadTreeView.prototype.CheckedNodeValues = function() {
var checkedNodes = $(this).get_checkedNodes();
for (var n in checkedNodes) {
checkedNodes[n].get_value();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.