0

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));
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
Dustin Baker
  • 43
  • 1
  • 1
  • 3
  • Have you tried checking for console errors? – arcyqwerty Nov 06 '15 at 19:12
  • 4
    `CoffeeType` doesn't exist where you are trying to access it. Did you mean to put the `}` after `this.location = location;` instead of before `document.write`? It doesn't make a lot of sense to call `Coffee` instead itself. `CoffeeType` also doesn't expect any argument and there is no variable with name `coffee`. Overall this seems to be arbitrarily stitched together. Which tutorial are you following? I suggest to read http://eloquentjavascript.net/ instead. – Felix Kling Nov 06 '15 at 19:12
  • Have some literature on scope! http://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/ – Xogle Nov 06 '15 at 19:15
  • I'm not using a tutorial... Just trying to figure things out on my own... Thanks for the insight! – Dustin Baker Nov 06 '15 at 19:31
  • _"it doesn't seem to be working"_ is not a good enough problem statement. – Lightness Races in Orbit Nov 06 '15 at 21:37

3 Answers3

3

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).

Community
  • 1
  • 1
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
1

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) {
    ...
Andy Ray
  • 30,372
  • 14
  • 101
  • 138
1

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())
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
john dith
  • 51
  • 6