In this video, we will start covering object oriented programming in Javascript. There are several ways to do it – the first is called “classical inheritance”.
Here’s the link to the “inherit” function I use here:
https://github.com/nodejs/node-v0.x-archive/blob/master/lib/util.js#L634-L644
Using a constructor, a prototoype, and the “new” keyword, we can create multiple copies or “instances” of a module, each with unique properties, but shared functionality.
We can also implement inheritance in Javascript. If we create a new module, we can inherit the functionality of a parent module with a simple inheritance helper function.
Original source
44 responses to “Modular Javascript #6 – Classical Inheritance & OOP with JS”
can I use setPrototypeOf()?? isn't it the same?
Hi @LearnCode.academy, so which one of these methods would you suggest us using then? awesome videos by the way! 😀
i didnt understand prototype meaning can you please explain it ??
Uppercase is just convention, correct? It has no functional impact
Pretty awesome video series ! Great content ! 🙂
had exactly the same reaction to 'prototype', lol.
Could we use this instead of that super inherit function from node?
Friend.prototype = Object.create(Person.prototype);
Friend.prototype.constructor = Friend;
awesome!
Dude, it took me like 2 hours to find out that your methods doesnt let children overwrite parent's functions… There really needs to be a disclaimer added.
Thanks for making this video! Although it would be nice if you went deeper into the #inherits method and what the function actually does in relation to the Javascript language.
Learn how programmers make friends @6:56 ..
My man… I don't think there is classical inheritance in JS. To me this is still considered prototypal inheritance.
the super part is super confusing
Effing amazing playlist! I've had so many lightbulb moments watching these. Big question though…how much of this changes with ES6? Enough to make some of it obsolete or did ES6 just provide another way of doing it?
Hello Learncode Academy my name is George and i want to make a programme with javascript that use nodejs and some other libs beacause the programme is gona be very large i want to seperate the code in modules or plugins, i watch your videos and i am comfuse what is the write way : to use classical inheritance – OOP or pupsub
what i want is to have a CORE mudule – the modules and the libs like jquery bootstrap what is the best for that web app??
Thank you.
with making constructors and instances can you like in your object literal pattern of javascript create "Private" attributes and methods? Also i've been reading classical inheritance in javascript is frowned upon now? is this true, i keep hearing use prototypal inheritance but i feel like its a blend of classical too so whats the big diff? other than writing methods on the prototype lol, you can tell i am pretty confused. i come from ruby btw and learning js now
When should I use prototypal inheritance versus object literal pattern? in your opinion? great videos btw!
Thanks for your help. But I have a question:
if we placed var Musician = function() {… after inherits()
The page give a message: belly.getInstrument is not a function(…)?
What going on for this?
es6 has the class syntax like you see in other languages
10:28 what is that IDE magic?
Thanks for great tutorial, this really helped.
ReferenceError: inherits is not defined
What is super_ exactly? It kind of seems like prototype, but that doesn't really make sense. Why would there be two classes from which the ctor object inherits from?
For those familiar with Java or C#, saying Person.call(this, name) is the equivalent of saying super(name).
Can you teach us how to organize a big/small javascript project. With some documentation things like i saw in that nodejs file:
/**
* Inherit the prototype methods from one constructor into another.
*
* The Function.prototype.inherits from lang.js rewritten as a standalone
* function (not on Function.prototype). NOTE: If this file is to be loaded
* during bootstrapping this function needs to be rewritten using some native
* functions as prototype setup using normal JavaScript does not work as
* expected during bootstrapping (see mirror.js in r114903).
*
* @param {function} ctor Constructor function which needs to inherit the
* prototype.
* @param {function} superCtor Constructor function to inherit prototype from.
*/
Keep going! This kind of video's is what the world needs.
BOBBY!!!
hey i do prototypal inheritance differently is this ok? and whats the difference btwn what you did?
“` var Musician = function(sounds) {
this.sounds = sounds;
};
Musician.prototype.solo = function(length) {
var solo = "";
for (var i=0; i<length; i++) {
solo += this.sounds[i % this.sounds.length] + " ";
}
console.log(solo);
};
var Guitarist = function() {
Musician.call(this, ['Twang', 'Thrumb', 'Bling']);
this.strings = 6;
};
Guitarist.prototype = Object.create(Musician.prototype);
Guitarist.prototype.constructor = Guitarist;
Guitarist.prototype.tune = function() {
console.log('Be with you in a moment');
console.log('Twoning sproing splang');
};
var Bassist = function() {
Musician.call(this, ['Boink', 'Bow', 'Boom']);
this.strings = 4;
};
Bassist.prototype = Object.create(Musician.prototype);
Bassist.prototype.constructor = Bassist;
var nigel = new Guitarist();
nigel.tune();
nigel.solo(8);
var derek = new Bassist();
derek.solo(4);
“`
loving your videos and series! keep the good work, this is some good material, helping me heaps. cheers
Thanks for the clear video. As a backend developer I am horrified by this though xD Must be the hackiest way of OOP I have ever seen (and I write a lot of PHP… o.o').
So i think many things have changed with ES6 now with classes right??
It's very helpful series for JS beginner like me, i like your presentation, it's very easy to understand, clearly and effective.
Thanks a lot.
doesn't ES6 have a built-in 'inherits'? superclass or something or other.
EDIT: looks like extends copies over methods and attaches them directly to the object, so no, they don't quite do the same thing. one thing i didn't understand about inherits is how it created Musician as a new instance of Person. i'd expected to see something like ctor.super_ = new superCtor; but it was missing the 'new' keyword.
does inherits do the same thing as underscore.js _.extends?
What makes the inherits function special instead of simply using Musician.prototype = Object.create(Person.prototype)?
Thank you so much for creating such content rich videos.
Can you expand upon how best practices from module programming such as a clean API extends to the realm of OOP? I guess im confused if I can combine both of these together and if so how would I implement an API in a class?
If Person takes two arguments, 'name' and 'age' for example, how would Musician inherit both of those? Because when Musician.super_.call() is called you're passing through 'this' and the argument 'name', but you don't seem to identify which property gets asigned. Or is that automatically determined by the name of the argument that is passed into Musician?
Thank you VERY much!!! Very helpful videos^)
"Hi my name is Bobby!"
lol, thanks for the laugh
Idk if you've heard about a material design framework called Materialize. I'd really like it if you do a video on it, seeing as there aren't many out there.
Really enjoying all your videos. They are very easy to understand and have helped me to understand concepts I've been struggling with for awhile now. Thanks!
Awesome lesson, Will! I've been looking for that in a while and I never got it clear as now. Waiting for the next one! Cheers!
Love all your videos
Great video! But I have a question:
Why do we create method of prototype by assining a function to Person.prototype.sayName instead of assigning it inside the constructor scope? (it would look like:
var Person = function(name){
this.name = name;
this.sayName = function(){..}
}
I read about this recently on MDN (Person, Employee, Customer, Mime example), but your video goes the extra mile by including the "inherits" function with data/accessor descriptors. Very informative and useful material. Thanks.