how to do inheritance in JS?

Hi guys I do know how to make Inheritance in Ruby but how to do that in JS?

I do know you can do that like that in ruby

class child_class < Parent_classbut how to do that in JS?
Javascript's inheritance doesn't work like Ruby's (or like most other mainstream languages).  You'd probably do something like this.

var Parent = function (item1) { this.item1 = item1;}var Child = function (item2) { this.item2 = item2;}Child.prototype = Parent;There are also libraries that make this process easier.  I use https://github.com/javascript/augment to get something similar to classical inheritance in Javascript.
I see so in simple you use Prototype ?

it's seem weird though this will extend the child classes?
I see so in simple you use Prototype ?

it's seem weird though this will extend the child classes?
It does seem weird at first.  The way it works is that Child.prototype is the parent of Child, so if you set Child.prototype to Parent, then Child has access to Parent's properties.  Every object that can be created with new has a prototype, so you can actually do stuff like Child.prototype.prototype to get the parent of Child's parent, and so on.  IIRC, the parent of Object is Object itself, and all non-inherited classes gain Object as a prototype, so eventually you just get Object back.
I see ...wow lol this confused me a little but so in simple if I have this

function Scene_MenuBase() { }

and then this

function Scene_Menu() {}

how I could create a inheritance?  (even if I think doing function like that don't wokrs well)
This is why I suggest using something like the Augment library I posted above.  Actually keeping inheritance working in vanilla Javascript is somewhat odd.

But anyway, to answer your question:

function Scene_MenuBase() {}function Scene_Menu() {}Scene_Menu.prototype = Scene_MenuBase;var menu = new Scene_Menu();Or, since apparently this uses ES5, you can use this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
I highly recommend you run through this short Udacity course covering scope/closures and inheritance/object-oriented programming in JS if you are not familiar with it: https://www.udacity.com/course/object-oriented-javascript--ud015
The `create` syntax seems pretty intuitive.


I hope this is what they use in MV's script system.
On the topic of inheritance, I know this is a bit much but you can do something like:

var ClassName = { // Your code here, see example below: someFunctionName: function(param) { ... }, someOtherFunctionName: function() { ... }}// Then using underscore.js: http://underscorejs.org/#extendvar NewClassName = _.extend(ClassName { // All of ClassName functions are included now newFunctionName: function() { ...}});// Now we can do:NewClassName.someOtherFunction();NewClassName.newFunctionName();// We cannot do:ClassName.newFunctionName();This is a bit more advanced yes I know, I might have even typed something wrong (oops) and it does require you to use underscore.js, which can be done through the concepts of Gulp to concatenate your underscore code ABOVE the example code I showed so that you have access to _.extend

-- You don't need gulp but its a good tool to understand for future js development
In ES6 you can also do Object.assign which pretty much does the same thing as Underscore's _.extend. I think Lodash is generally preferred to Underscore these days as well (but they're basically the same in functionality). The downside with this 'mixin' approach is memory usage.

function Scene_MenuBase() {}function Scene_Menu() {}Scene_Menu.prototype = Scene_MenuBase;var menu = new Scene_Menu();
I should have noticed this earlier, but setting a constructor's .prototype to a function (even if it is a constructor) is a very bad idea. I mean your newly-created object would inherit methods from Function.prototype which makes no sense. If you did `menu instanceof Function` it would return true, and `menu instanceof Scene_MenuBase` would return false, enough said.

The closest thing that would make any sort of sense is `Scene_Menu.prototype = Scene_MenuBase.prototype;`, but even this is pretty messed up and shouldn't be used for many reasons.

This is not the way to go about inheritance in JavaScript. Go through the course I linked further up, they explain things much better than I can,
In ES6 you can also do Object.assign which pretty much does the same thing as Underscore's _.extend. I think Lodash is generally preferred to Underscore these days as well (but they're basically the same in functionality).

The downside with this 'mixin' approach is memory usage.

 

I should have noticed this earlier, but setting a constructor's .prototype to a function (even if it is a constructor) is a very bad idea. I mean your newly-created object would inherit methods from Function.prototype which makes no sense. This is not the way to go about inheritance in JavaScript. Go through the course I linked further up.

the problem with prototype is that it can be a bit hard to read as your application gets larger. While yes underscore does have a memory issue if you create large applications most people in the javascript industry don't generally work with prototypes, they work with objects that are extended and states that are immutable.
the problem with prototype is that it can be a bit hard to read as your application gets larger. While yes underscore does have a memory issue if you create large applications most people in the javascript industry don't generally work with prototypes, they work with objects that are extended and states that are immutable.
It all depends on what you're doing/trying to achieve. A game is one of the few contexts where I think classes (aka constructors) and .prototype can be quite beneficial, because game-making lends itself well to object-oriented design. For the average web application, maybe not so much. But don't forget that you can do prototypal inheritance without classes/class inheritance/.prototype by just using Object.create() with plain objects - many people that disagree with classes in JS have no problem with this - you don't need to go all the way to extending objects with mixins every time you want to share functionality between them, although it is certainly a valid technique with its own advantages.

I just wanna add that the backlash against classes and inheritance in JS (especially ES6 class syntax) is relatively recent. While there are some good reasons for it, OO in JS has been used for many years and isn't going away any time soon.
It all depends on what you're doing/trying to achieve. A game is one of the few contexts where I think classes (aka constructors) and .prototype can be quite beneficial, because game-making lends itself well to object-oriented design. For the average web application, maybe not so much. But don't forget that you can do prototypal inheritance without classes/class inheritance/.prototype by just using Object.create() with plain objects - many people that disagree with classes in JS have no problem with this - you don't need to go all the way to extending objects with mixins every time you want to share functionality between them, although it is certainly a valid technique with its own advantages.

I just wanna add that the backlash against classes and inheritance in JS (especially ES6 class syntax) is relatively recent. While there are some good reasons for it, OO in JS has been used for many years and isn't going away any time soon.

 A game is one of the few contexts where I think classes (aka constructors) and .prototype can be quite beneficial, because game-making lends itself well to object-oriented design.

Um how is my way of creating classes, in fact the common way of creating classes in may javascript frameworks not OOP where as your prototype is? Yours is more functional and abstract where as mine is more concrete and defined, albiet not in the typical language constructs. While yes prototyping can have OOP based concepts it does tend to be a bit more of a mess as the application or in this case the script grows.

For the average web application, maybe not so much. But don't forget that you can do prototypal inheritance without classes/class inheritance/.prototype by just using Object.create() with plain objects - many people that disagree with classes in JS have no problem with this - you don't need to go all the way to extending objects with mixins every time you want to share functionality between them, although it is certainly a valid technique with its own advantages.

It is and its the way most javascript frameworks like Ember and React do it its also the way most common web applications are built, I have yet to see one that uses prototype and its various functions to do any kind of extension of objects and classes and so on and so forth. prototype was a thing before jquery and after jquery it still hasnt been a thing ... a major thing that is ...

I just wanna add that the backlash against classes and inheritance in JS (especially ES6 class syntax) is relatively recent. While there are some good reasons for it, OO in JS has been used for many years and isn't going away any time soon.

There is no backlash at all ... Not sure what that is coming from. OO in JS is rather new concept its still not pure OO as we know it, there is more of a move towards functional programming ever since languages like rust came into view though ... 

But this concept that prototype based functions and functionality makes your code OO seems .... sketchy too me.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-do-inheritance-in-js.45300/