2016-12-08 9 views
0

prototype constructorthisにアクセスしようとしています。コンストラクタオブジェクトでこれにアクセスする

SomethingUseful.prototype = { 
    constructor: SomethingUseful, 
    init: init, 
    someFunction: someFunction, 
    mouseDownHandler: mouseDownHander.bind(this) 
} 

しかしthiswindowに等しいです。だから、私はbind(SomethingUseful)を実行しようとしました。mouseDownHandlerに私がthisを書き込むと、関数全体がプレーンテキストとしてログアウトするので、実際のmouseDownHandler関数ではthisにあるものは使用できません。

constructor機能にSomethingUsefulthisにアクセスするための正しい方法は(再び、私はおそらく名前が間違っていましたし、私が行った場合、それを修正する、または私がコメントでお知らせ下さい)何ですか?

+1

あなたは正確に何を達成しようとしていますか? 'this'の詳細な説明はこれを見てください。 @stottoverflow.com/questions/40722379/callback-with-this-confusion/40722549 –

+0

@ScottMarcusこれを行うのではなく、 'this.mouseDownHandler = mouseDownHander.bind(this);'これを 'SomethingUseful .prototype = {'。これが明確になることを願っています。 – Jessica

+1

[あなたのプロトタイプ定義は変です](http://stackoverflow.com/q/17474390/1048572)。また、プロトタイプ上のインスタンスにメソッドをバインドすることはできません。定義中にインスタンス( 'this')がなく、メソッドが共有されるためです。代わりにコンストラクタでそれを行います! – Bergi

答えて

1

この代わりに、this.mouseDownHandler = mouseDownHander.bind(this);SomethingUseful.prototype = {に追加します。

これはできません。関数をインスタンスにバインドしようとしている瞬間に、関数をバインドできるインスタンスはありません。コンストラクターが呼び出されている間またはその後にのみ、関数をバインドするインスタンスが存在します。

他の言葉:ピザが納品される前に食べようとしています。


関連:How to access the correct `this` context inside a callback?

+0

実際、それは調理される前にピザを食べようとするよりもそうです。 –

+0

'somethingUseful'の新しいインスタンスを作成すると、コンストラクタが呼び出されますか? – Jessica

+0

'SomethingUseful' *はコンストラクタです(あなたは' function SomethingUseful(){...} 'のようなものがあると思います)。 –

1

が定義オブジェクトになると、thisがインスタンスからインスタンスに変わる値を有することができる特性である「インスタンスのプロパティ」を宣言するために使用されます。

通常、関数(別名メソッド)を格納するプロパティの場合、その機能はインスタンスごとに変更されないため、これらのプロパティは通常オブジェクトのプロトタイプで作成されるため、各インスタンスは厳密に他のすべてのインスタンスと同じ機能です。

オブジェクトを後で作成するように設計するときは、オブジェクトを呼び出すことができないため、オブジェクトリテラルではなく関数を宣言します。

// This is known as a "constructor function" because it is 
 
// intended to produce (construct) objects that will then 
 
// be bound to instance variables: 
 
function SomethingUseful(){ 
 
    // This is a regular variable that is scoped to the function 
 
    // it will not be accessible to instances later. 
 
    var a = 10; 
 
    
 
    // Because of the word "this", this becomes an "instance property" 
 
    // that each instance of SomethingUseful will have. Each instance 
 
    // will start out with a default value of 20, but each instance 
 
    // can change that value and it won't affect any other instance 
 
    this.someProperty = 20; 
 
} 
 

 
// Methods are not set up in the constructor (so they do not become 
 
// instance properties) instead, they are set up on the constructor's 
 
// prototype and inherited into each instance. All instances share 
 
// the same one prototype: 
 
SomethingUseful.prototype.someMethod = function(){ 
 
    // Even though this method is not stored with each instance, it 
 
    // can access the current instance by using "this" 
 
    return this.someProperty; 
 
}; 
 

 

 
// Make instances of SomethingUseful by calling the constructor function 
 
// which returns object instances. Those instances are stored in different 
 
// variables and that's how we keep the instances separate from each other 
 
var obj1 = new SomethingUseful(); 
 
var obj2 = new SomethingUseful(); 
 

 
// Now that we have used the constructor function to create the instances, 
 
// we can ask those instances what function was used to create them: 
 
console.log("The object stored in obj1 was constructed with: " + obj1.constructor.name); 
 

 
//Check obj1: 
 
console.log("obj1.someProperty is: " + obj1.someProperty); 
 
obj1.someProperty = 50; 
 
console.log("obj1.someProperty changed and is now: " + obj1.someProperty) 
 
console.log("obj1.someMethod returns: " + obj1.someMethod()); 
 

 
//Check obj2: 
 
console.log("obj2.someProperty is: " + obj2.someProperty); 
 
console.log("obj2.someMethod returns: " + obj2.someMethod());

は、JavaScriptはあなたのための世話をする何かの世話をしようとしています。必要な機能をイベントプロパティで定義し、次にオブジェクトのインスタンスを作成します。 JavaScriptは、各インスタンスがあなたの関数を取得することを確認します。

関連する問題