--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
It's an idiom from callbacks in client side js since way back (first
time I used it was with XHR back in 2004). :)
Thanks,
James
Just 2¢,
–Jacob
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
--
function bind(d, c) {
var a = Array.prototype.slice.call(arguments, 2);
var b = function () {
var f = d || (this == window ? false : this),
e = a.concat(Array.prototype.slice.call(arguments));
if (typeof(c) == "string") {
if (f[c]) return f[c].apply(f, e);
} else return c.apply(f, e);
};
if (typeof c == 'string') {
b.name = c;
} else if (c && c.name) b.name = c.name;
return b;
}
Function.prototype.bind = function (b) {
var a = [b, this].concat(Array.prototype.slice.call(arguments, 1));
return bind.apply(null, a);
}
And you write :
setTimeout(function(){ this.do_tuff_with }.bind(this), 10);
Or :
something.click(this.method.bind(this, 1234));
And not :
var self = this;
something.click(function(){
self.method(1234);
});
2010/10/27 Ricardo Tomasi <ricar...@gmail.com>:
> I like turtles.
>
> The self, one's "das Über-Ich", comprises the organised part of the
> personality structure.
>
> "The super-ego retains the character of the father (...)"
> —Freud, The Ego and the Id (1923)
>
> It is very common in jQuery land, where you're usually dealing with a
> most important element in the code block:
>
> $('.allsomething').each(function(){
> var self = this;
>
> $(self).find('a').hover(function(){
> makeFries( self );
> }, function(){
> self.analyze();
> });
>
> });
>
> In node you'll usually be dealing with a lot more deep nesting, so
> 'self' will often not be the best choice, but it's the same idea.
>
> On Oct 26, 11:47 pm, Ryan Gahl <ryan.g...@gmail.com> wrote:
>> Also, I like semicolons;
>>
>> NEW GAME: saying "I like(don't like) semicolons;" === Rick Rolling the
>> audience while pointing out banality of continuing current discussion; that
>> it has morphed into a redundant or purely opinion based state.
>>
>> On Tue, Oct 26, 2010 at 7:49 PM, Dominic Tarr <dominic.t...@gmail.com>wrote:
>>
>>
>>
>>
>>
>>
>>
>> > ah, good point
>>
>> > if something called your callback using fn.apply(newThis,args) the value of
>> > 'this' would be unexpected,
>>
>> > so to be safe using self = this is a good idea.
>>
>> > otherwise you could get some very hard to diagnose bugs.
>>
>> > On Wed, Oct 27, 2010 at 1:32 PM, Orlando Vazquez <ovazq...@gmail.com>wrote:
>>
>> >> On Tue, Oct 26, 2010 at 4:03 PM, Dominic Tarr <dominic.t...@gmail.com>
>> >> nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
>> >> .
>> >> For more options, visit this group at
>> >>http://groups.google.com/group/nodejs?hl=en.
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups
>> > "nodejs" group.
>> > To post to this group, send email to nod...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
something.click => @method(1234)
instead of:
var self = this;
something.click(function(){
self.method(1234);
});
2010/10/27 CC <coman...@gmail.com>:
In this case it doesn't matter, window.self == this in global scope,
which in turn this == window in global scope. See:
> self
DOMWindow
> this
DOMWindow
> this == window
true
> this == window.self
true
I'd see that as an endorsement for self = this;
Yours,
Micheil Smith
--
BrandedCode.com
@Ryan Gahl, I know you're trying to reduce unnecessary bike-shedding and some of these threads probably annoy you, but it's not the same thing. The thread about OO styles is very informative to many people new to javascript, and the OO style you use does have a real impact on both the readability AND the runtime behavior of your code. In this thread, while what you name the "self" or "me" variable has little consequence, the technique of using it is very powerful and often required for a lot of use cases and styles. Instead of saying this conversation is meaningless help steer it toward the important points, like what does "this" mean in JavaScript, why are closures important, and especially why does this matter to a node.js programmer.
The thing is that discussions like these can clear up misapprehensions and increase the competence of the people who ultimately contribute to the node ecology. People shouldn't fear to ask questions lest they be sniped by a douche with a penchant for semicolons; That node performance test thread? Same thing. I did a similar thing in the macOS kernel panic thread but it was atleast marginally funny (and I did feel like a douche for doing it).
Thanks :)
--i
> @Ryan Gahl, I know you're trying to reduce unnecessary bike-shedding and some of these threads probably annoy you, but it's not the same thing. The thread about OO styles is very informative to many people new to javascript, and the OO style you use does have a real impact on both the readability AND the runtime behavior of your code. In this thread, while what you name the "self" or "me" variable has little consequence, the technique of using it is very powerful and often required for a lot of use cases and styles. Instead of saying this conversation is meaningless help steer it toward the important points, like what does "this" mean in JavaScript, why are closures important, and especially why does this matter to a node.js programmer.
>
> @Dominic Like others have said, the purpose of "var self = this" is to store a reference to the old "this" value since it will always be overwritten in an inner function scope. It's a simple use of closures. It's very important to me that people have a sound understanding of these concepts and so I have written quite a bit about it.
>
> http://howtonode.org/what-is-this
>
> http://howtonode.org/why-use-closure
>
> http://howtonode.org/object-graphs
>
> http://howtonode.org/object-graphs-2
>
> "this" is a funny thing in JavaScript, it has nothing to do with class object instances or methods or anything to do with "self" and "this" in other classical OO languages. It's controlled 100% by how the function is called. Having a good understand of both what determines the value of "this" in a function body and what variables are available in the closures are both essential to writing event based systems in JavaScript. "var self = this" is a simple technique where you're bridging a value from the "this" scope to the closure scope via a local variable.
I save this in that, me self :-)
Cheers,
P.S. ES5 attempts to fix some of this flaws (11.2.3 Function Calls).
--
Jorge.
> No, that's an endorsement for "var self = this;", not "self = this;"
>
> It's easy to forget that 'var' keyword, as you generously demonstrate,
> which leads to important (and undesirable) behavior changes.
>
> And even when things are done correctly, this practice casts doubt on
> what each 'self' reference in the code refers to. Is it the window or
> something else? The only way to know for sure is to painstakingly
> read back through all the closure contexts to see if there's a "var
> self = ..." assignment somewhere. It's a pain in the ass.
>
> So, no, the one specific case where this works w/out issue (and is
> completely unnecessary anyways) is hardly an endorsement.
I agree with Robert.
I don't use 'self', I use 'that', and every JS programmer knows what a 'that' means.
--
Jorge.
When writing something like that in an english sentence, it's perfectly fine to
leave off var imho. Seriously, deadhorses anyone?
– Micheil
Seriously, deadhorses anyone?
I'm really hoping the missing "var self, that, foo, _this,
thisAndThat;" line to scope those vars locally was just fabulous humor
on your part, cause otherwise you're making this way too easy. :-)
--
var a = b = 1
creates a var "a" and a *global* "b".
--i
var a = b = 1
creates a var "a" and a *global* "b".
I'm not playing along, I'm just saying it's getting kinda silly now, it's been
explain and everyone has gone into almost anal detail. There's no need
to keep bickering about it, really.
Yours,
Micheil Smith
--
BrandedCode.com
I use self as well, habit from python, but window.self seems to
endorse using self for js.
Those poor horses, unicorns, narwhals and fluffy kittens that died in
the making of this thread.
On Fri, Oct 29, 2010 at 5:25 AM, dipti seni <dipp...@gmail.com> wrote:
>
> www.ksrista.com
> search your life partner
>