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 trialYuval Blass
18,134 PointsA question about prototype...
Hi everyone,
I finished the Object Oriented js course a moment ago, and I have a question; When we use x.prototype = Object.create(X)
? Actually, What does it do?
Best,
Yuval Blass.
1 Answer
Charles Kenney
15,604 PointsYou can think of prototype as a property of a given object. It, itself is an object, which describes the prototypal methods and properties of the actual object model. The easiest way to explain this is to try it in the browser. Open up the javascript console in chrome and try the following:
// An Object
function MyObject() {
this.foo = 'bar';
}
// A Prototype Method
MyObject.prototype.bar = function() {
return 'foo' + this.foo;
}
MyInheritedObject = Object.create(MyObject.prototype);
console.log(MyObject);
As a result, you should see an object of the following shape in your console:
MyObject {
__proto__: {
bar: function(),
constructor: function MyObject(),
__proto__: Object
}
}
There you can see the method we defined, and the constructor function. This is what you are assigning to your inherited object's prototype property.
Hope this helps,
- Charles