I have following script
<script>
var Obj = {
purpose_1: {
defaults: {
purpose:{ my_variable:"Test" }
},
activity: function() {
console.log( Obj.purpose_1.defaults.purpose.my_variable );
}
},
purpose_2: {
}
}
Obj.purpose_1.activity();
</script>
I am accessing my_variable
and getting output Test
in console from line
console.log( Obj.purpose_1.defaults.purpose.my_variable );
Is there any short cut way like
this.my_variable
to access my_variable
instead of this long path
Obj.purpose_1.defaults.purpose.my_variable
Thanks.
activity: function () {
return function() {
//this now refers to Obj.purpose_1.defaults.purpose
console.log(this.my_variable);
}.call(Obj.purpose_1.defaults.purpose);
// call overrides the context in which the function is executed
}
Return a function bound to the context you need! Here is a sample fiddle.
See the Function.prototype.call
method at MDN.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다