So this is why I see "that" and similar variables on popular frameworks. Good to know. Anyway, I think the bind() option works better or at lest nicer. Though I didn't test it enough yet, I guess bind() isn't used in old and popular scripts because it wasn't supported until IE9.
2013/10/11 Alejandro Lechuga <ing.ramon.alejandro@gmail.com>
If you want reference from the context you need to do something like this
function MyCtor() { var a; var b; var that = this; var myFunct = function() { console.log(1,that); } }
new MyCtor();On Fri, Oct 11, 2013 at 4:17 PM, Alejandro Lechuga <ing.ramon.alejandro@gmail.com> wrote:
Because functions don't hold the context just by scope unless they are part of one context like a property
function MyCtor() { var a; var b; var myFunct = function() { console.log(1,this); } console.log(2,this); myFunct(); console.log(3,this); // part of the context this.myFunct2 = function() { console.log(4,this); } this.myFunct2(); } new MyCtor(); // Instance call MyCtor(); // Function callOn Fri, Oct 11, 2013 at 3:45 PM, Fábio de Godoy <mexicano21@gmail.com> wrote:
Misterious, you're right, I didn't expect this:However, if you really need a "private" function inside a constructor where "this" refers to the object you can use bind():--2013/10/10 Les Hartzman <lhartzman@gmail.com>
Hi,--I've got a JS function used as a constructor. The HTML file includes code to 'new' the constructor to create the object.My understanding is that variables defined within the constructor should have the context of the constructor and not of 'window'. But in my case what I'm seeing is that within a declared function in the constructor, the context is 'window':function MyCtor(){var a;var b;var myFunc = function() {debugger; //#2// code for function}// more codedebugger; //#1myFunct();debugger; //#3} //end MyCtorWhen execution breaks at debugger #1, 'this' refers to 'MyCtor'. However, at debugger #2 this refers to 'window'. And at debugger #3 it is 'MyCtor'.What happened to the MyCtor context within myFunc? Clearly I'm missing something basic here.Thanks.Les
You received this message because you are subscribed to the Google Groups "JavaScript Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javascript-information+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Fábio de Godoy
http://www.animenewsnetwork.com/MyManga/?user=mexicano21
--
You received this message because you are subscribed to the Google Groups "JavaScript Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javascript-information+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Fábio de Godoy
http://www.animenewsnetwork.com/MyManga/?user=mexicano21
You received this message because you are subscribed to the Google Groups "JavaScript Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javascript-information+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.