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:
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
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();
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();
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();
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.