Loading ...
Sorry, an error occurred while loading the content.

2690Re: [jslint] this question

Expand Messages
  • stauren
    Nov 20, 2011
    • 0 Attachment
      I don't think this is about block scope or functional sope. This is
      probably because the function is executed when the script is being parsed,
      at which time the outer object is not fully established, so the the keyword
      'this' points to the window object.

      Another funny aspect of javascript.

      ---
      stauren (畅)
      blog : http://stauren.net
      email : stauren@...


      On Sat, Nov 19, 2011 at 12:19 AM, <z_mikowski@...> wrote:

      > **
      >
      >
      > Javascript does not provide block scope, only functional scope. Therefore
      > the 'this' in your code takes the value as provided to the enclosing
      > function. You could use the module pattern to get the results you are
      > seeking, e.g.
      >
      > var o = function (){
      > // private stuff
      > return {
      > p : "a",
      > // rest of object here
      > };
      > };
      >
      >
      > Nagy Endre <forewer2000@...> wrote:
      >
      > >
      > >Hi everyone, maybe it's a little offtopic sorry, but can anyone explain
      > why "this" inside the "o" object is refer to window ?
      > >Why it's not refer to the "o" object if we are inside there? I want to
      > access from the returned object in function "f" the "o.p" property without
      > using "o". Thanks!
      > >
      > >var p = 2,
      > > o = {
      > > p: "a",
      > > f: (function (m) {
      > > return {
      > > l: m.p
      > > };
      > > }(this))
      > > };
      > >
      > >p = o.f.l;
      > >
      > >Endre
      > >
      > >
      > >[Non-text portions of this message have been removed]
      > >
      >
      > [Non-text portions of this message have been removed]
      >
      >
      >


      [Non-text portions of this message have been removed]
    • Show all 22 messages in this topic