2017-08-16 10 views
0

Javascriptでのプロパティのアクセスについて、非常に簡単な質問があります。これまで最も簡単なものになるはずですが、これが動作しない理由を私は理解カント:Javascriptで関数内のプロパティを呼び出す

私はオブジェクトがあります。

var myObj = { 
    first: "First text", 
    second: "Second text", 
    third: "Third text" 
}; 

をそして、私は最初のプロパティを呼び出したい:

console.log(myObj.first); // First text 

すべて大丈夫。しかし、私は、関数からこのプロパティを呼び出すと、また新しい価値"Fourth text"で新しいプロパティ"Fourth"を割り当てたい:あなたが見ることができるように

function myFunction(myObj){ 
    console.log(this.first); // [object Object] undefined 
    this.fourth = "Fourth text"; 
} 
myFunction(myObj); 
console.log(myObj.fourth); // undefined 

、最後の2つの呼び出しが未定義になります。なぜ私は理解できないのですか?任意のヘルプは非常に高く評価されます:)

答えて

1

myObjをあなたの関数に渡しますが、thisとして使用しました。 this.firstの代わりにfunctionmyObj.firstのパラメータを使用します。

var myObj = { 
 
    first: "First text", 
 
    second: "Second text", 
 
    third: "Third text" 
 
}; 
 

 
function myFunction(myObj){ 
 
    console.log(myObj.first); 
 
    myObj.fourth = "Fourth text"; 
 
} 
 

 
myFunction(myObj); 
 
console.log(myObj.fourth);

それとも、thisを使用してアクセスする場合は、あなたがあなたのmyObjにコンテキストをバインドする必要があります。たとえばcallと、最初の引数として、あなたのオブジェクトを渡します。関数内のthisは、渡されたオブジェクトを参照します。あなたがオブジェクトにアクセスする必要が

var myObj = { 
 
     first: "First text", 
 
     second: "Second text", 
 
     third: "Third text" 
 
    }; 
 

 
function myFunction(){ 
 
    console.log(this.first); 
 
    this.fourth = "Fourth text"; 
 
} 
 

 
myFunction.call(myObj); 
 
console.log(myObj.fourth);

+0

はい、私は関数はオブジェクトと関連していないことを忘れてしまった、と私はそれを関連付ける必要があります。しかし、別の問題があります.Google Chromeのコンソールの結果は、あなたのものとは異なります。たとえば、私があなたの解決策を実行すると、 'undefined undefined'と' undefined'が得られます... – Nikita

+0

気をつけてください。あなたがChromeで書いた間違い。あなたのコードをもう一度見て、それは未定義ではありません –

+0

はい、問題は私がmyFunctionをいくつかの場所で定義していることです。何らかの形でそれは何かに混じっていた。 'var myFunction = function(){...}' – Nikita

1

myObj.first 
^^^^^ 

function myFunction(myObj){ 
 
    console.log(myObj.first); 
 
    myObj.fourth = "Fourth text"; 
 
} 
 

 
var myObj = { 
 
    first: "First text", 
 
    second: "Second text", 
 
    third: "Third text" 
 
}; 
 

 
myFunction(myObj); 
 

 
console.log(myObj.fourth);

すでにmentioneを持っているとして、あなたは結果

function myFunction() { 
 
    console.log(this.first); 
 
    this.fourth = "Fourth text"; 
 
} 
 

 
var myObj = { 
 
    first: "First text", 
 
    second: "Second text", 
 
    third: "Third text" 
 
}; 
 

 
myFunction.bind(myObj)(); 
 

 
console.log(myObj.fourth);
のために呼ばれるためにある新しい関数を生成する関数、にオブジェクトをバインドすることができ

+0

ありがとう@nina、私は 'this'を使いたかったのですが、関数が私が参照していたオブジェクトを知らなかったので、 call'、 'apply'、' bind'はそれを行います! :) – Nikita

1

関数内myObjを使用して、関数のthis

var myObj = { 
 
    first: "First text", 
 
    second: "Second text", 
 
    third: "Third text" 
 
}; 
 

 
//use myObj as this of function 
 
function myFunction(){ 
 
    console.log(this.first); // "First text" 
 
    this.fourth = "Fourth text"; 
 
} 
 
myFunction.call(myObj); 
 
console.log(myObj.fourth); // "Fourth text"

としてmyObjを使用して

var myObj = { 
 
    first: "First text", 
 
    second: "Second text", 
 
    third: "Third text" 
 
}; 
 

 
//use myObj inside a function 
 
function myFunction(myObj){ 
 
    console.log(myObj.first); // "First text" 
 
    myObj.fourth = "Fourth text"; 
 
} 
 
myFunction(myObj); 
 
console.log(myObj.fourth); // "Fourth text"

+0

関数myFunction(myObj){ console.log(myObj.first); //実際のデータを出力します myObj.fourth = "4番目のテキスト"; //代入 } myFunction(myObj); //関数内でアクションを呼び出して実行する console.log(myObj.fourth); // "第4のテキスト" – xqoo0ooq

+1

回答を編集しました – qiAlex

+0

私はすでに見ましたが、ごめんなさい – xqoo0ooq

1

あなたの問題は、 "この" withiの使用方法はあなたの機能。 (この詳細についてはJavaScriptでhttps://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/this

あなたの機能はあなたのオブジェクトの一部ではありません。私はあなたがすでにあなたのオブジェクトをパラメータとして与えているのを見ますが、決してそれを使用しません。 「this」の代わりに、パラメータを使用する必要があります。

var myObj = { 
    first: "First text", 
    second: "Second text", 
    third: "Third text" 
} 


    function myFunction(myObj){ 
    console.log(myObj.first); 
    myObj.fourth = "Fourth text"; 
    } 


myFunction(myObj); 
console.log(myObj.fourth); 

あなたは「これは」、あなたはそれを行うことができます使用したい場合:

var myObj = { 
    first: "First text", 
    second: "Second text", 
    third: "Third text", 
    myFunction: function(){ 
    console.log(this.first); 
    this.fourth = "Fourth text"; 
    } 
} 

myObj.myFunction(); 
console.log(myObj.fourth); 
+0

私はこれを使用したいと思う場合、2つのオプションがあります:関数はオブジェクトの中になければなりません、または関数をオブジェクトに関連付けるために呼び出し、適用、またはバインドを使用する必要があります:) – Nikita

+0

はい、それが明らかであることを聞いて幸せ:D –

関連する問題