SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: this and window

  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    this and window

    Hi.
    What's the difference (if there is) between
    this and window both are globals, isn't it ?
    For instance
    PHP Code:
    function xhr(){ 
            return 
    this.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP"); 
        };
    function 
    xhr(){
            return 
    window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
        }; 

    Bye.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    That depends on how it's called. The this keyword refers to the object that made the call and so can be the window, some other function, or the element itself, or depending on how the function is called.

    See http://www.quirksmode.org/js/this.html
    Last edited by paul_wilkins; Apr 20, 2008 at 16:25.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are returning the constructor (function) if the client has a native XMLHttpRequest object,
    but an instance (object) if the client is IE6. Shouldn't both alternatives return the object?

    function xhr(){

    if(window.XMLHttpRequest) return new XMLHttpRequest() ;
    try{
    return new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(er){
    //
    }
    return null;
    }

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies.
    In this snippet what's could be the different:

    PHP Code:
    ABC = (function(ABC){
        return  function(
    dataurlasyncuserpass){
           
    //code
        
    };
    })(
    this.XMLHttpRequest ?
        function(){return new 
    XMLHttpRequest;} :
        function(){return new 
    ActiveXObject("Microsoft.XMLHTTP");}
    ); 
    Bye.

  5. #5
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I saw the light it refers to
    a function property

    Bye.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •