Whether you think the global object being `this` is an error or not, it doesn't change the fact these implied references to the global scope create hard to track down bugs just my forgetting a single keyword some random place in the code.
`this` makes perfect sense as undefined. You're creating a new object, `this` is that object. You're calling a method on an object, `this` is that object, you're calling foo() which you just defined in your local scope, the global has nothing to do with `this`, it's not the object you are calling it on, you're calling it on nothing, so your this is nothing. `this` still refers to the global object in global scope; var originalWindow = this; If you are desperate to use that function this hack it's perfectly possible as well. var originalWindow = (function(){return this})(); (function(window) { "strict mode"; // strict mode code with a safe window })(originalWindow); But honestly, what possible kind of js programming have you done where you're not writing your code in the global scope? JS doesn't make it easy, or very possible to dynamically nest code inside of another unrelated set of code. It's all run in global scope unless you use eval. ~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name] Andrea Giammarchi wrote: > Daniel, > I do not think the global object as default this is an error at all, > this is my point. > > What does not make sense at all is to use a "this" referred to an > undefined value. > > undefined is not an object so "this" which is a self-instance/scope > pointer does not make sense. > > This is the most secure way to obtain the original global object and > we are loosing it with "strict" > > var originalWindow = (function(){return this})(); > > so old browser with "strict" code cannot retrieve the native window if > it has been redefined somewhere else. > > Do you understand what I mean? > > "strict" will mean double test, double behavior, because of compatible > and not compatible browser ... it is truly simple to understand, you > resolve "gotchas" ? Trust me, you gonna create even more gotchas for > old browsers in this way. > > Regards > > > On Fri, Aug 14, 2009 at 12:48 PM, Daniel Friesen > <nadir.seen.f...@gmail.com <mailto:nadir.seen.f...@gmail.com>> wrote: > > > Strict mode doesn't have new "features", it has restrictions. > Strict mode adds nothing over what ES3 already has, it only places > restrictions on things which can cause issues or hinder the > ability for > the engine to optimize. > this in functions is undefined instead of global, eval cannot > dynamically inject new variables, eval cannot be renamed or passed > around in ways that make eval get indirectly called and potentially > inject code where it doesn't belong, `foo = 123` does not define an > implied global and throws instead, while a nice feature but widely > misunderstood while() {} prevents some potential optimizations so > it's a > syntax error in strict mode, there might have been one or two more. > > Your code works fine whether it's run in ES3 or ES5. The difference is > that when testing using an ES5 browser a lot of gotchas like > forgetting > var in front of a variable throw errors instead of silently exhibiting > confusing behavior. As well any ES5 browser running the code has the > opportunity to optimize the code and run it faster. > > ~Daniel Friesen (Dantman, Nadir-Seen-Fire) > [http://daniel.friesen.name] > > Andrea Giammarchi wrote: > > Who talked about users here? > > > > You put "strict" you do not have that common behavior, whatever > right > > or wrong it is, but you cannot use new features for > compatibility reason. > > > > Where exactly do you find a better development and debug pattern > with > > this strategy? > > > > > > > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---