4

いくつかの新しい関数を追加し、すでに存在するいくつかの関数をオーバーライドするために拡張したい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'); }
}
4

1 に答える 1

7

この記事が役に立ちます: 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');
  }
});

ここにフィドルがあります:http://jsfiddle.net/gqWDC/

于 2012-04-27T00:59:47.983 に答える