...

View Full Version : Accessing local objects?



bunzu
11-10-2012, 08:05 PM
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!

Old Pedant
11-10-2012, 09:19 PM
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


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

Object.test();

????

bunzu
11-11-2012, 05:02 AM
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 :(



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


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

Object.test();

????

felgall
11-11-2012, 10:46 PM
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:


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

Object.test();

bunzu
11-12-2012, 12:05 AM
Still doesn't seem to work :(




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:


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

Object.test();

Old Pedant
11-12-2012, 12:57 AM
I think Felgall was all wet on that answer.

This works, by using a closure:



<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:

test: setInterval( anything, anytime )
and then expecting to do

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

var test = setInterval( ... );

The most you could then do with test would be something like

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.

Old Pedant
11-12-2012, 01:04 AM
Remember, in literal object notation, the 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:


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.

bunzu
11-12-2012, 06:47 PM
Thanks. Let me give this a try and will let you know the result. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum