0

私はJSのOOPに少し慣れていません。サブオブジェクトを作成するときに、サブオブジェクトの2番目のレベルの後でメインオブジェクトの参照を停止する理由を知りたいです。

function Clase()
{
    this.__construct = function()
    {
    this.paginator();            
    alert('__construct finished');
    };                

    this.paginator = function()
    {

        this.paginator.title = function()
        {
            this.paginator.title.set_offsets  = function()
            {
                alert('paginator.title.set_offsets executed!');

            };
        };

        this.paginator.title(); //instantiating

        alert('subobject paginator created');            
     };

    this.__construct();
}

var instancia = new Clase();

instancia.paginator.title.set_offsets();

http://jsfiddle.net/WYWwE/

エラーは次のとおりです。this.paginatorが未定義です。

そして今、私がクロージャを使用する場合、それは完全に機能します:

function Clase()
{
    self = this;

    this.__construct = function()
    {
        this.paginator();            
        alert('__construct finished');
    };                

    this.paginator = function()
    {

        self.paginator.title = function()
        {
            self.paginator.title.set_offsets  = function()
            {
                alert('instancia.paginator.title.set_offsets() executed');

            };
     };
     self.paginator.title();

     alert('this.paginator created');
};

this.__construct();
}

var instancia = new Clase();

instancia.paginator.title.set_offsets();

http://jsfiddle.net/esjHu/

したがって、ある時点でAFAIKは、「this」はクラス「Clase」の参照を停止し、他の何かを参照します。もしそうなら、このようにクロージャを使用するのは良い習慣ですか?

self=thisでクラスを開始することも正しいですか。それ以降は「自己」のみを使用しますか?例: http: //jsfiddle.net/byGRX/

4

2 に答える 2

3

thisネストすると、「元の」への参照が失われますfunction。対処するには、次の手順を実行します。

function Clase() {
    var that = this;


    this.paginator = {

        title: {

            set_offsets: function() {
                alert('paginator.title.set_offsets executed!');

            }
        }
    };
};

var foo = new Clase();

foo.paginator.title.set_offsets();​

http://jsfiddle.net/vd5YK/

于 2012-09-10T17:34:00.973 に答える
0

thisオブジェクトへの参照を失うことはありません。次のようになります。

例えば:

function Class() {

  this.func1 = function () {

    this.func1.func2 = function () {
      alert('Works!');
    };

  };

  this.func1.func2();
}

x = new Class();

さて、func2存在しないというエラーが表示される理由は、func2を呼び出すまで関数オブジェクトが構築されないためfunc1です。

function Class() {

  this.func1 = function () {

    this.func1.func2 = function () {
      alert('Works!');
    };

  };

  this.func1();
  this.func1.func2();
}

x = new Class();

そして今、それは機能します。

編集:

それで、なぜこれが機能しないのですか?

function Class() {

    this.func1 = function() {

        this.func1.func2 = function() {

            this.func1.func2.func3 = function() {
                alert('works!');
            };

            this.func1.func2.property = 5;
        };

    };

    this.func1();
    this.func1.func2();
}

x = new Class();

x.func1.func2.func3();

基本的に、あなたがしようとしているのは、という名前のプロパティとという名前propertyのメソッドfunc3をの関数オブジェクトに追加することfunc2ですが、問題は、をfunc2呼び出す前に構築されないことfunc1です。それは行うことと同じです:

function Class() {

    this.func1 = function() {

        this.func1.func2 = function() {};

    };

    this.func1.func2.func3 = function() {
        alert('works!');
    };
    this.func1.func2.property = 5;

    this.func1();
    this.func1.func2();
}

x = new Class();

x.func1.func2.func3();

それを機能させたい場合は、最初に以下func2を呼び出して関数オブジェクトを作成する必要がありfunc1ます。

function Class() {

    this.func1 = function() {

        this.func1.func2 = function() {};

    };

    this.func1();

    this.func1.func2.func3 = function() {
        alert('works!');
    };
    this.func1.func2.property = 5;

    // this.func1.func2();

}

x = new Class();

x.func1.func2.func3();
alert(x.func1.func2.property);
于 2012-09-10T18:33:48.770 に答える