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 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Accessing local objects?

    Hey Guys,

    I'm running into a bit of problem accessing my objects. I'm trying not to duplicate code base or update multiple instances of the same number. This is what I'm trying to do:

    Working:
    Object = {
    test: {
    time: new Date().getTime,
    duration: 2000,
    test: setInterval(function(){
    console.log("hi hi");
    }, 2000) }
    }
    Object.test();


    Not working:
    Object = {
    test: {
    time: new Date().getTime,
    duration: 2000,
    test: setInterval(function(){
    console.log("hi hi");
    }, this.duration) }
    }
    Object.test();

    Notice I changed the 2000 to this.duration hoping I can just set the duration once.

    Any help would be great.
    Thanks!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Ummmm... Object.test is the entire sub-object named test within Object.

    To call the function test, you would need to do Object.test.test( )

    Why do you have the extra layer of another object in there? Why not just
    Code:
    var Object = {
        time: new Date().getTime, 
        duration: 2000, 
        test: setInterval(function(){console.log("hi hi");}, this.duration)
    }
    
    Object.test();
    ????
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I wanted it to do this way but "this.duration" returns undefined when using it with test: setInterval(function(){console.log("hi hi");}, this.duration);

    so confused


    Quote Originally Posted by Old Pedant View Post
    Ummmm... Object.test is the entire sub-object named test within Object.

    To call the function test, you would need to do Object.test.test( )

    Why do you have the extra layer of another object in there? Why not just
    Code:
    var Object = {
        time: new Date().getTime, 
        duration: 2000, 
        test: setInterval(function(){console.log("hi hi");}, this.duration)
    }
    
    Object.test();
    ????

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    this goes out of scope when the original object code has run and so when the setInterval runs it points to an entirely different object which doesn't have a durationproperty.

    The usual way to keep this in scope after the object code has run is to set up another field (usually called self) that will continue to point to the current object in the subsequent code.

    Try this:

    Code:
    var Object = {
        time: new Date().getTime, 
        duration: 2000, 
        self: this,
        test: setInterval(function(){console.log("hi hi");}, self.duration)
    }
    
    Object.test();
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Still doesn't seem to work



    Quote Originally Posted by felgall View Post
    this goes out of scope when the original object code has run and so when the setInterval runs it points to an entirely different object which doesn't have a durationproperty.

    The usual way to keep this in scope after the object code has run is to set up another field (usually called self) that will continue to point to the current object in the subsequent code.

    Try this:

    Code:
    var Object = {
        time: new Date().getTime, 
        duration: 2000, 
        self: this,
        test: setInterval(function(){console.log("hi hi");}, self.duration)
    }
    
    Object.test();

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    I think Felgall was all wet on that answer.

    This works, by using a closure:

    Code:
    <script type="text/javascript">
    function foo(d)
    {
        alert("Bingo: " + d);
    }
    
    var Object = {
        time: new Date().getTime, 
        duration: 5000, 
        test: function() { var tick = this.duration; setTimeout(function(){ foo(tick); }, tick); }
    }
    
    Object.test();
    </script>
    But your original code, just doing:
    Code:
        test: setInterval( anything, anytime )
    and then expecting to do
    Code:
        Object.test( );
    would never work: In your code, test is *NOT* a function. It is, instead, the RESULT of calling setInterval( ) WHEN Object IS DEFINED!

    In other words, the same as doing
    Code:
        var test = setInterval( ... );
    The most you could then do with test would be something like
    Code:
        clearInterval( Object.test );
    You *NEED* to REFERENCE a function if you want to be able to call Object.test( ) and that's what I have now done.

    And thanks to the closure (that is, var tick = this.duration;) you now have all the elements you need.
    Last edited by Old Pedant; 11-12-2012 at 12:06 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    bunzu (11-12-2012)

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Remember, in literal object notation, the code
    Code:
        member : value
    is essentially an assignment of value to member.

    Si only if value is a REFERENCE to a function will you then be able to use member( ). If value is a CALL to a function, the member will have the value of the result of that call *AND* the call will be made when the object is defined.

    So normally, to have a member *be* a function you need to use eitther:
    Code:
        member : someFunctionName /* notice NO PARENTHESES! */
    or
        member : function( optional, arguments ) { body of function }
    As you can see, setInterval( x, y ) *is* a function call, not a function reference.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New Coder
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks. Let me give this a try and will let you know the result.


  •  

    Posting Permissions

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