1

I am working my way through understanding how to create a constructor with properties and methods. The one below I have written and tested but it does not work. Could someone take the time to help me understand what it is that would make this not work. Understand that I have searched Google, I am reading books, etc., but needs some hands on support in understanding the concept with creating my own. Thank you.

function ball( type, grip, shape ) {
  this.type = type;
  this.grip = grip;
  this.shape = shape;
  this.caught = function( who,how ) {
    this.who = who;
    this.how = how;
  };
  this.player = function() {
    return (who + "caught the ball" + how + "that was a" + type + "shaped like 
            a " + shape + "thrown with a" + grip);
    };
};

var baseball = new ball("Mickey Mantle","sliding","baseball","circle","fastball");

console.log(ball);

Edit: From the answers below - thank you for sharing - I have created my jsfiddle and can't comprehend why the caught property is not working. How am I supposed to set the attributes for this method??

http://jsfiddle.net/uYTW6/

3 Answers 3

1

In your player function, you need to reference the variables who, how, type, shape and grip using this, i.e.

return (this.who + "caught the ball" + this.how + "that was a" + this.type + "shaped like 
        a " + this.shape + "thrown with a" + this.grip);
};

Furthermore, functions common to all objects of type ball should be put into the prototype, so that the function only will be created once:

ball.prototype.player = function() {
    return (this.who + "caught the ball" + this.how + "that was a" + this.type + "shaped like a " + this.shape + "thrown with a" + this.grip);
    };
}

It is also common convention to start the constructor function name with an uppercase letter, like B in your case (so your constructor function's name is Ball, not ball).

Updated answer

You forgot to call the caught function on the baseball object, like so:

var baseball = new Ball("Mickey Mantle","sliding","baseball","circle","fastball");
baseball.caught('me', 'by hand');
Sign up to request clarification or add additional context in comments.

Comments

1

http://jsfiddle.net/rvMNp/1/

You should do console.log(baseball) to get the current object.

Also on my fiddle you will notice that your player function does not work as expected. This is because quite a few of your variables are undefined.


new ball("Mickey Mantle","sliding","baseball","circle","fastball");

This is calling the ball function with 5 variables, yet your ball function only accepts 3

function ball( type, grip, shape )


You also need to use this.* for any variables defined in the function like this:

return (this.who + "caught the ball" + this.how + "that was a" + this.type + "shaped like a " + this.shape + "thrown with a" + this.grip);

Comments

0

You only need to do console.log(baseball) instead of console.log(ball) to see the object you created. Also, you need quotes in the end and in the beginning of the return lines in the player function, like this:

return (who + "caught the ball" + how + "that was a" + type + "shaped like "+
        "a " + shape + "thrown with a" + grip);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.