扩展而不覆盖
我正在设法使 XUI 和 XUI 之间兼容。 Backbone,但我在设计中遇到了问题。我猜。
就在那里。我想使用方法调用 attr
来扩展 XUI,该方法能够处理属性/值
的哈希值。 jQuery 做到了,Backbone 也利用了它。这就是我想这样做的原因。
不幸的是,XUI 中已经有一个 attr
方法。所以,当我这样做时:
xui.extend({
attr: function (attributes) {
if (typeof attributes == "object") {
for (var attr in attributes) {
this.attr(attr, attributes[attr]);
}
};
}
});
当然,XUI 的原型只有一种 attr 方法。矿。我怎么能有两个呢?
可以做以下事情:
xui(element).attr('attr', 'value');
xui(element).attr({'attr': 'value', 'foo': 'bar'});
感谢您的阅读和帮助:)
I'm in a way to make compat between XUI & Backbone but I met a problem in my design. I guess.
There it is. I want to extend XUI with a method call attr
which would be able to deal with a hash of attributes/values
. jQuery does, and Backbone exploits it. It's why I want to do this.
Unlucky, there already is an attr
method in XUI. So, when I do:
xui.extend({
attr: function (attributes) {
if (typeof attributes == "object") {
for (var attr in attributes) {
this.attr(attr, attributes[attr]);
}
};
}
});
Of course, the proto of XUI have just one attr method. Mine. How can I have two?
Something doing following available:
xui(element).attr('attr', 'value');
xui(element).attr({'attr': 'value', 'foo': 'bar'});
Thanks for reading and helping :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要保存对原始函数的引用,然后查看调用该函数的参数以确定您应该调用自己的版本还是原始版本。
像这样的东西(未经测试的代码,所以仅用于灵感):
You need to save a reference to the original function, and then look at the arguments the function is called with to determine weather you should call your own version or the original.
Something like this (not tested code, so use only for inspiration):