10

これは、JavaScriptの決定的なガイドから直接、JavaScriptで「クラス」またはコンストラクター関数を記述する教科書の標準的な方法です。

function Rectangle(w,h) {
    this.width = w;
    this.height = h;
}
Rectangle.prototype.area = function() { 
    return this.width * this.height;
};

ここでぶら下がっているプロトタイプの操作は好きではないのでarea、コンストラクター内の関数定義をカプセル化する方法を考えようとしていました。私はこれを思いついたが、これはうまくいくとは思っていなかった。

function Rectangle(w,h) {
    this.width = w;
    this.height = h;
    this.constructor.prototype.area = function() { 
        return this.width * this.height;
    };
}

this関数内の参照は関数自体areaを指している必要があるため、これが機能するとは思っていませんでした。そのため、にアクセスしたり、からアクセスしたりすることはできません。しかし、私はそうすることがわかりました!areawidthheightthis

var rect = new Rectangle(2,3);
var area = rect.area(); // great scott! it is 6

さらにいくつかのテストにより、関数this内の参照がarea実際にはエリア関数自体ではなく、構築中のオブジェクトへの参照であることが確認されました。

function Rectangle(w,h) {
    this.width = w;
    this.height = h;
    var me = this;
    this.constructor.prototype.whatever = function() { 
        if (this === me) { alert ('this is not what you think');}
    };
}

アラートがポップアップし、thisまさに建設中のオブジェクトであることがわかります。では、ここで何が起こっているのでしょうか。なぜ私はそれが期待されないthisのですか?this

4

5 に答える 5

5

正しい答えは、kennebecがコメントで言ったように、これを行うべきではないということだと思います。

長方形が100個ある場合は、そのプロトタイプメソッドを100回再宣言します。

于 2010-03-24T16:25:20.593 に答える
1

「これ」は常に関数が呼び出されたオブジェクトを指していると思いました。

于 2010-03-11T22:20:29.200 に答える
1

の意味はthis、関数がどのように呼び出されたかによって異なります。

  • this通常、関数が実行時に呼び出されるオブジェクトを参照します。たとえば、として呼び出されるとob.foo()thisfooはobを参照します。
  • 関数がオブジェクト提供なしの方法で呼び出された場合、たとえばfoo()thisは、グローバル変数(jsプログラム内の他のすべてのグローバル変数を含む最上位のオブジェクト)を参照します。
  • そして、.call().apply()、を参照するオブジェクトthisが提供されます。

ここで、関数が作成された関数のオブジェクト(つまり、作成時の2番目のレベル)を指す方法が必要な場合はthis、より深い名前を変更しthisて、現在の関数を輝かせる必要があります。目に見えるもの。それが泥のようにはっきりしている場合、これは少し明確にするのに役立つはずです:

function outside() {
    // Here, 'this' refers to the object outside() is called with.
    // Let's "rename" this, to make it visible to inside functions.
    var that = this,
        private = 42;

    return {
        inside: function {
            // here, 'this' refers to the object inside() is called with,
            //  it is hiding outside's this.
            // Also, 'that' refers to the object outside() was called with,
            //  *at the time* outside was called and inside was created.
            // Notice that you can see 'private'
            // (but nobody outside of 'outside()) can!
            return private;
        }
    }
}

上記のこのパターンは、プライベートメンバーにアクセスできるパブリックメソッドを使用してオブジェクトを作成する場合に役立ちます。おそらくより良い説明については、 Crockfordを参照してください。

于 2010-03-12T22:05:38.403 に答える
0

'this'が何を指すかは、コードが実行されるときに決定されます。

これが「これ」がどうあるべきかを理解する簡単な方法です。

'。'を使用する場合 関数を参照するには、「this」は「。」の左側にあるものすべてへの参照になります。

(これは.callと.applyを考慮していません)

var myFn = function () {
return this.name + ' called me';
};

var me = {
name : 'oatkiller',
shoutOut : myFn
};

var you = {
name : 'Matthew',
shoutOut : myFn
};

// im on the left of the dot, so this points to me
me.shoutOut(); // 'oatkiller called me'

// youre on the left of the dot, so this points to you
you.shoutOut(); // 'Matthew called me'
于 2010-03-12T21:30:39.683 に答える
0

あなたは最後の例に本当に近いと思います。特権メソッドを使用するのはどうですか。

function Rectangle(w,h) {
  this.width = w;
  this.height = h;
  var that = this;
  this.area = function() { 
    return that.width * that.height;
  }
}

var rect = new Rectangle(2,3);
console.log(rect.area());

詳細については、Crockfordの「JavaScriptのプライベートメンバー」を参照してください。

于 2010-03-17T14:29:34.307 に答える