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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    OOP passing variables

    Having trouble passing the variables name age species and breed into function displayBadge from Pets.

    Any help would be appreciated! Thank you in advance!
    Code:
    	species = prompt("What species of a pet did you bring today?",ES);
    	breed = prompt("What is the breed of your pet?",ES);
    	age = prompt("How many years old is your pet?",ES);
    	name = prompt("What is your pet's name",ES);
    	
    	var competingPet = new Pet(name, age, species, breed);
    	
    	competingPet.displayBadge();
    Code:
      function Pet(theName, theAge, theSpecies, theBreed) {
    	this.petName = theName;
    	this.petAge = theAge;
    	this.petSpecies = theSpecies;
    	this.petBreed = theBreed;
    
            this.displayBadge = displayBadge;
      }
      
      function displayBadge() {
    	document.write(petName + " is a " + petBreed + " " + petSpecies +
            " and is " + petAge + " years old.");
      }
    Last edited by Anrk; 07-23-2012 at 11:15 PM. Reason: Answered my own question. Incase anyone needs help i added (this.petName etc to function displayBadge() docwrite.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    a) an object’s methods belong into its prototype (i.e. the prototype property of its constructor function) and should reference the object properties, not some global variables from outside.

    b) the displayBadge() function will eat your page if called after page load (an often overlooked side effect of document.write())
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    Jul 2012
    Location
    Ukraine
    Posts
    71
    Thanks
    1
    Thanked 18 Times in 17 Posts
    You cannot reference an object's properties inside a method by using simple identifiers in Javascript, unlike some other programming languages. Use the this keyword inside a method to refer to the object on which the method was called.
    Code:
      function displayBadge() {
    	document.write(this.petName + " is a " + this.petBreed + " " + this.petSpecies +
            " and is " + this.petAge + " years old.");
      }

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,572
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Look at the "last edited" comment at the bottom of the first post.

    This thread should be closed as RESOLVED.
    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.


  •  

    Posting Permissions

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