2017-11-16 29 views
0
var a = {"b": {"c": function() {console.log(this);}}}; 
var x = {"b": "I should be 'this'"}; 

a.b.c(); 
+0

あなたはこれまでに何をしようとしたのですか? – JohnPan

+0

@Vucko、あなたの編集について、セミコロンは必須ではありませんが、間違っているわけではないので、投稿に残すべきだと思います。 –

+0

@FedericoklezCullocaそれは 'キャッチされないでSyntaxErrorを返します:予期しないトークンを;' – Vucko

答えて

4

このオブジェクトをバインドするか、このオブジェクトとしてプロパティを取得できます。

文字列に変換しなくても、あなたはこのコンソールでオブジェクトのような配列を取得します。

方法:

  • Function#bind

    The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

  • Function#call

    The call() method calls a function with a given this value and arguments provided individually.

マベは、あなたも、ここを見て:this

var a = { b: { c: function() { console.log(this.toString()); } } }, 
 
    x = { b: "I should be 'this'"}; 
 

 
a.b.c.bind(x.b)(); 
 
a.b.c.call(x.b);

関連する問題