Results 1 to 4 of 4

Thread: Problems with self calling object function

  1. #1
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Problems with self calling object function

    If anyone can help with this I woulg be grateful.
    I think this is the offending line:
    Code:
    setTimeout("this.walk()",5000)
    Here is the full code:
    Code:
    <script>
    function person()
    	{
    	this.walk = walk;
    	this.move=0;
    	this.position = 0;
    	}
    	
    function walk()
    	{
    	this.position += 2;
    	if(this.position<= 10)
    		{
    		alert("Walking")
    		setTimeout("this.walk()",5000)
    		}
    	else
    		{
    		alert("Finished walking")
    		}
    	}
    
    James = new person();
    James.walk();
    </script>
    Something wrong with calling the same object function from within itself?

    Cheers

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <script>
    The type attribute is required.
    Code:
    function person()
    ...
    James = new person();
    The convention for constructor functions is that they start with a capital, and the convention for variables is that they don't. In English we use capitals for proper nouns; in Javascript, we don't.
    Code:
    this.walk = walk;
    This is what prototypes are for.
    Code:
    setTimeout("this.walk()",5000)
    If you pass a string to setTimeout() (which is bad practice), it will be evaluated in the global scope, so this isn't defined. Instead, you can store a reference to the current object in a variable and use a closure:
    Code:
    <script type="text/javascript">
      function Person() {
        this.move = 
          this.position =
          0;
      }
            
      Person.prototype.walk = function() {
        this.position += 2;
        if(this.position <=  10) {
          alert("Walking");
          var me = this;
          setTimeout(function() { me.walk(); }, 5000);
        } else
          alert("Finished walking")
      };
    
      var james = new Person();
      james.walk();
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Thanks

    Hey Twey,

    Thanks for the help.
    I have never used anything but strings in setIntervals and setTimeouts :S, so that is a real education.

    One little thing: you say it is evaluated in the global scope, yet you initiate the variable 'me' with var, making it local. Or am I confused?

    Thanks again.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    One little thing: you say it is evaluated in the global scope, yet you initiate the variable 'me' with var, making it local.
    A closure is able to access the local variables in the scope in which it was created no matter where it's called. That's the defining feature of a closure. If we made "me" global, it could lead to race conditions.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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