Re: Newbie question on scope and construtor

Friday, October 11, 2013

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 call


On 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 code

    debugger;  //#1
    myFunct();
    debugger; //#3

} //end MyCtor

When 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.

Copyright © 2010 Web Design | Free Blogger Templates by Splashy Templates | Layout by Atomic Website Templates

Vida de bombeiro Recipes Informatica Humor Jokes Mensagens Curiosity Saude Video Games Animals Diario das Mensagens Eletronica Rei Jesus News Noticias da TV Artesanato Esportes Noticias Atuais Games Pets Career Religion Recreation Business Education Autos Academics Style Television Programming Motosport Humor News The Games Home Downs World News Internet Design Entertaimment Celebrities 1001 Games Doctor Pets Net Downs World Enter Jesus Mensagensr Android Rub Letras Dialogue cosmetics Genexus lasofia thebushrajr wingshock tripedes gorduravegetal dainfamia dejavu-transpersonal jsbenfica republicadasbadanas ruiherbon iranianforaryans eaystcheyl fotosdanadir Só Humor Curiosity Gifs Medical Female American Health Madeira Designer PPS Divertidas Estate Travel Estate Writing Computer Matilde Ocultos Matilde futebolcomnoticias girassol lettheworldturn topdigitalnet Bem amado enjohnny produceideas foodasticos cronicasdoimaginario downloadsdegraca compactandoletras newcuriosidades blogdoarmario arrozinhoii