Dustin Baker
Dustin Baker

Reputation: 53

JavaScript object will not print

I am trying to print out my object but it doesn't seem to be working. I'm very new to coding and am not sure as to what I am doing wrong here. Please help!

function Coffee(name, roast, location) {
    this.name = name;
    this.roast = roast;
    this.location = location;

    var esspresso = new Coffee("Starbuck's Esspresso Blend", "Dark", "Seattle");
    var cappuccino = new Coffee(coffee.name(espresso), "Dark", "Seattle");

    var CoffeeType = function() {
        return coffee.name + " " + coffee.roast + " from " + coffee.location;
    };
}
document.write(CoffeeType(cappuccino));

Upvotes: 0

Views: 65

Answers (3)

john dith
john dith

Reputation: 51

Try this:

function Coffee(name, roast, location) {

    this.name = name;
    this.roast = roast;
    this.location = location;
    this.CoffeeType = function() {
        return this.name + " " + this.roast + " from " + this.location;
    };

}

var esspresso = new Coffee("Starbuck's Esspresso Blend", "Dark", "Seattle");
var cappuccino = new Coffee("espresso", "Dark", "Seattle");

console.log(esspresso.CoffeeType())

Upvotes: 1

arcyqwerty
arcyqwerty

Reputation: 10675

If you're shooting for an object oriented approach, you should be defining your functions on the prototype chain.

function Coffee(name, roast, location) {
    this.name = name;
    this.roast = roast;
    this.location = location;
}

Coffee.prototype.getCoffeeType = function() {
    return this.name + " " + this.roast + " from " + this.location;
};

var esspresso = new Coffee("Starbuck's Esspresso Blend", "Dark", "Seattle");
var cappuccino = new Coffee(esspresso.name, "Dark", "Seattle");

document.write(cappuccino.getCoffeeType());

The code above will have the following effects:

  • function Coffee(...) is the class's main constructor.

  • Coffee.prototype.getCoffeeType is a function that each instance of the Coffee class will have. This function will be able to access the this variables of the class (the class members).

  • esspresso and cappuccino are instances of the Coffee class, instantiated somewhere in program code (outside of the class definition / constructor / prototype functions).

  • You can then call functions defined on the class's prototype chain on these instance objects.

  • The output of the above code will give you "Starbuck's Esspresso Blend Dark from Seattle"

Note that the additional benefit of defining a function on the prototype is that all instances will share the same getCoffeeType function instead of having a separate function (which does the same thing) on each instance. (see Use of 'prototype' vs. 'this' in JavaScript?). If you're running a large coffee chain and producing millions of coffees, then you'll probably save a fair amount of resources (i.e. memory).

Upvotes: 3

Andy Ray
Andy Ray

Reputation: 32046

CoffeeType is defined only in the scope of Coffee, and can't be used outside of the { }. You also need to pass in the coffee as an argument to use it. var CoffeeType = function( coffee ) {.

Change your program to define CoffeeType in a higher scope so it can be used outside of Coffee's body:

function CoffeeType(coffee) {
    return coffee.name + " " + coffee.roast + " from " + coffee.location;
};

function Coffee(name, roast, location) {
    ...

Upvotes: 1

Related Questions