Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBryan Simca
9,470 Pointsmethods() in object
does the method() declared in the class, the speak()
method, defined as a property in an object? when you console.log(ernie), there was never a speak()
method as its property
1 Answer
Liam Clarke
19,938 PointsHello Bryan,
Classes in JavaScript are built on prototypes
class Ernie {
speak() {
return 'Hello, my names Ernie';
}
}
To console log a class method, you will need to access it through the prototype
object.
console.log(console.log(Ernie.prototype.speak)) // ƒ speak() { return 'Hello, my names Ernie'; }
However, If you create a new object instance of Ernie, you can access the speak as you would expect
console.log(new Ernie().speak()) // Hello, my names Ernie