1

コードは次のようになります

function Scripts() {this.FindById = function (id) {
    this.FindById.constructor.prototype.value = function () {
        return document.getElementById(id).value;

    }}}

var Control = new Scripts();

今私がControl.FindById( "T1")。value()と言うとき。textInput( "T1")の値を取得できません。

4

2 に答える 2

1

あなたのコードはもう少し複雑なようです;-)

個人的に私はそれをこのように書くでしょう(テストされていません):

function Scripts() {
  this.findById = function(id) {
    var el = document.getElementById(id);

    return {
      value: function() { 
        return el.value;
      }
    }
  }
}

これfindById()で、ノードが閉じられ、その値を返すことができるインターフェースが返されます。

また、あなたのアイデアはシングルトンによく似ているので、追加のScriptsコンストラクターも必要ありません。

var Control = {
    findById: function(id) {
        var el = document.getElementById(id);

        return {
            value: function() { 
                return el.value;
            }
        }
    }
}

実例: http: //jsfiddle.net/YYkD7/

于 2012-06-13T05:21:20.297 に答える
0

これを試して:

function Scripts() {this.FindById = function (id) {
    this.FindById.constructor.prototype.value = function () {
        return document.getElementById(id).value
    }}}

最後の「}」を閉じていません:-)

于 2012-06-13T05:27:09.637 に答える