Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-10-2012, 07:05 PM   PM User | #1
bunzu
New Coder

 
Join Date: Jul 2012
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
bunzu is an unknown quantity at this point
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!
bunzu is offline   Reply With Quote
Old 11-10-2012, 08:19 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-11-2012, 04:02 AM   PM User | #3
bunzu
New Coder

 
Join Date: Jul 2012
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
bunzu is an unknown quantity at this point
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();
????
bunzu is offline   Reply With Quote
Old 11-11-2012, 09:46 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 11-11-2012, 11:05 PM   PM User | #5
bunzu
New Coder

 
Join Date: Jul 2012
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
bunzu is an unknown quantity at this point
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();
bunzu is offline   Reply With Quote
Old 11-11-2012, 11:57 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
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.

Last edited by Old Pedant; 11-12-2012 at 12:06 AM..
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
bunzu (11-12-2012)
Old 11-12-2012, 12:04 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-12-2012, 05:47 PM   PM User | #8
bunzu
New Coder

 
Join Date: Jul 2012
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
bunzu is an unknown quantity at this point
Thanks. Let me give this a try and will let you know the result.
bunzu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:47 AM.


Advertisement
Log in to turn off these ads.