Skip to main content

JavaScript "this" confusion



This is probably pretty easy, yet I am confused, so maybe I might still learn something more today. I am trying to do the following:







var myObj = {};

myObj.src = {}

myObj.src.A = ['1.png','2.png','3.png'];

myObj.A = new Image();

myObj.A.src = this.src.A[0];







This will result in an Uncaught TypeError: Cannot read property 'A' of undefined error. When I use myObj.src.A[0] instead of this it is working fine.





What would be the correct way to use this in this context?


Comments

  1. The this keyword is always different depending on the scope involved. In the code snippet you've posted above, assuming that you're just putting this in the document somewhere in the head, this refers to the page itself. So, it's fairly obvious at that point that this.src.A will be undefined. If you were to apply an event to it with a delegate such as:

    myObj.click = function() {
    alert(this.src.A[0]);
    }


    The this keyword receives a new scope belonging to the owner of the delegate (in this case myObj). this is very easy to track so long as your clearly define your scopes and your scope boundaries.

    ReplyDelete
  2. this refers to the object context in which the code is executing. So if an object has a method aMethod, then inside aMethod this references the object that owns it.

    I'm assuming your code there is running in the global namespace, so this is undefined. Really you just want myObj.A.src = myObj.src.a[0];.

    http://www.quirksmode.org/js/this.html

    ReplyDelete
  3. this does not refer to var myObj in the case of your code. It is likely that if you are not inside the scope of a function or an objects method then this is referring to the DOM window which has no attribute src.A

    ReplyDelete
  4. this in JavaScript depends heavily on the context in which a function is called. If your code is (as it looks to be above) just hanging out in a script tag in the page, then this is going to be the "global" context -- which in the browser is the window object.

    Generally, this refers to the object/scope a function belongs to, but there's a number of special (and useful) cases that happen because functions are first class values that can be assigned to different objects and invoked in various contexts.

    Some lengthy elaboration others have written might be helpful:


    http://stackoverflow.com/a/3320706/87170
    http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/
    http://net.tutsplus.com/tutorials/javascript-ajax/fully-understanding-the-this-keyword/
    https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/this_Operator


    It can seem a little tricky at the start, particularly if you're used to languages in which this is always one thing, but after you learn a few rules it becomes fairly straightforward and actually very useful.

    ReplyDelete
  5. myObj.src.A[0] would be correct in this context because this references its immediate parent.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex