Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    'this' is not actually this??

    Hi,

    Its been a long time since I did anything with js and it all escapes me now.

    I have two objects from different script files.

    For arguments sake...

    Code:
    function firstObject() {
     this.internalId = 'firstObject';
    
     this.addContent = function() {
       alert(this.internalId);
     }
     return this;
    }
    
    
    function secondObject() {
     this.internalId = 'secondObject';
     return this;
    }
    Now, when I create instances of my two objects and call addContent on my first object...

    Code:
     var first = firstObject();
     var second = secondObject();
     first.addContent(second);
    The alert I get for internalId is 'secondObject' when it should be 'firstObject'.

    I am obviously missing something here, any advice?

    Thanks in advance.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by outofsight View Post
    I am obviously missing something here[...]
    Yes. By short: to use a function as a constructor, you should call it using the token new
    Code:
    var first = new firstObject();
    var second = new secondObject();
    And so, there is no need to return the value (no need for return this within the constructor).
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor View Post
    Yes. By short: to use a function as a constructor, you should call it using the token new
    Code:
    var first = new firstObject();
    var second = new secondObject();
    And so, there is no need to return the value (no need for return this within the constructor).
    Doh, of course. You wouldn't believe the amount of hair pulled out over this and I knew it would come down to something simple.

    Thanks very much

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,412
    Thanks
    11
    Thanked 595 Times in 575 Posts
    if you want to be lazy, coerce the Constructor:
    Code:
    function secondObject() {
     if(this.RegExp==RegExp){return new arguments.callee }
     this.internalId = 'secondObject';
     return this;
    }
    you can cut and paste the red line in any constructor to guard against accidentally clobbering global variables.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

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