0

したがって、入力引数を使用して HTMLButtonElement のコンストラクターを再定義します。引数なしでこれを行う方法を知っています:

var CButtonPrototype = Object.create(HTMLButtonElement.prototype);
CButtonPrototype.createdCallback = function()
{
  alert("call");
  this.setAttribute("class", "some class");
  this.value = 0;
  this.innerHTML = "some text";
};

var CButton = document.registerElement('cbutton', {
  prototype: CButtonPrototype
});

var myButton = new CButton();

それは機能しますが、このクラスを次のように使用したいと思いますvar myButton = new CButton(arg 1, arg 2, etc);。この方法ではできませんCButtonPrototype.createdCallback = function(arg 1, arg2)。どうすればこれを解決できますか? 多分あなたは別の方法を知っていますか?

ありがとう\o/

4

1 に答える 1

1

この型を拡張する必要がある場合は、次の点を考慮してください。

CButton.prototype.test = function()
{
    console.log(arguments);
}

CButton.prototype.test2 = function(num, str, bool)
{
    console.log(num + ' ' + str + ' ' + bool);
}

myButton.test(20, 'hello', true); //call test function with parameters
myButton.test2(20, 'hello', true); // still the same

元の質問について:

この「関数」はシステム関数へのデリゲートにすぎないため、パラメーターを挿入することはできません...あなたの場合はオブジェクトc'torです。

それをテストするには、引数を試すことができます-関数の引数を表すjsのすべての関数内の特別な配列:

var CButtonPrototype = Object.create(HTMLButtonElement.prototype);
CButtonPrototype.createdCallback = function()
{
  console.log(arguments); // print arguments to the console screen
  this.setAttribute("class", "some class");
  this.value = 0;
  this.innerHTML = "some text";
};

var CButton = document.registerElement('cbutton', {
  prototype: CButtonPrototype
});

var myButton = new CButton();

このコードを実行すると、空の配列が表示されます。主な理由は、c'tor 呼び出し 'new CButton()' に引数がないためです。引数を挿入しようとすると、エラーが発生します。

于 2014-11-29T19:51:30.103 に答える