Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion what is var self = this; ?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Orlando Vazquez  
View profile  
 More options Oct 26 2010, 8:32 pm
From: Orlando Vazquez <ovazq...@gmail.com>
Date: Tue, 26 Oct 2010 17:32:25 -0700
Local: Tues, Oct 26 2010 8:32 pm
Subject: Re: [nodejs] what is var self = this; ?

On Tue, Oct 26, 2010 at 4:03 PM, Dominic Tarr <dominic.t...@gmail.com> wrote:
> what is the purpose of
> self = this
> I've seen it a bunch of times now, but not 100% sure what it's for.
> is it so that functions in a object retain access to the object's
> prototype/instance and would 'this' point to the prototype object of a
> function if it is not initialized with new?

Someone please correct me if I'm wrong, but JavaScript only has
function scope and global scope. If a variable is not found in the
current scope, JavaScript keeps going up in scopes until it finds that
variable or decides it doesn't exist. This is the case for most
variables, with the exception of `this` and `arguments`, which ALL
functions get. (There may be more, but I dont remember offhand)
Typically `this` can come from an instantiated object that's had a
method called on it, or someone doing a Fn.apply or Fn.call with an
explicit target object. (eg myfunction.apply(myobject, args) => this =
myobject)

Therefore, in order to "hold onto" a previous scope, we just assign it
to `self`. You may a few layers deep in callbacks involving multiple
objects, where it's useful to retain a handle on the top-most object
which you can use in callbacks. Like others have posted here, the name
`self` isn't magical (unlike `this`, which is), you could call it
whatever you want, it's just a convention that's sprung up...

--
Orlando Vazquez


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.