gpt4 book ai didi

javascript - 如何在完全独立的 .js 文件中覆盖/扩展 prototype.js 类

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:27 25 4
gpt4 key购买 nike

我有一个 prototype.js 类,我想扩展它以添加一些新函数并覆盖一些已有的函数。

在下面的示例中,我想添加 initAutocompleteNew 并编辑 initAutocomplete 以提醒"new"。

Varien.searchForm = Class.create();
Varien.searchForm.prototype = {
initialize : function(form, field, emptyText){
this.form = $(form);
this.field = $(field);
this.emptyText = emptyText;

Event.observe(this.form, 'submit', this.submit.bind(this));
Event.observe(this.field, 'focus', this.focus.bind(this));
Event.observe(this.field, 'blur', this.blur.bind(this));
this.blur();
},
//////more was here

initAutocomplete : function(url, destinationElement){
alert("old");
},
}

有人建议但那行不通我认为是 jQuery?

$.extend(obj_name.prototype, {
newfoo : function() { alert('hi #3'); }
}

最佳答案

这篇文章应该有所帮助:http://prototypejs.org/learn/class-inheritance

看起来您正在按照该页面第一个示例中描述的“旧”方式定义您的类。您使用的是 1.7 吗?

假设您使用的是 1.7,如果您想覆盖或向您的类添加方法,您可以使用 Class.addMethods :

Varien.searchForm.addMethods({
initAutocomplete: function(url, destinationElement) {
// Your new implementation
// This will override what was previously defined
alert('new');
},
someNewMethod: function() {
// This will add a new method, `someNewMethod`
alert('someNewMethod');
}
});

这是一个 fiddle :http://jsfiddle.net/gqWDC/

关于javascript - 如何在完全独立的 .js 文件中覆盖/扩展 prototype.js 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10338228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com