Inheritance in Javascript - protoyping not in definition-part?
NickName:Roman Ask DateTime:2009-07-30T17:08:24

Inheritance in Javascript - protoyping not in definition-part?

I am currently switching from AS3 to JavaScript.
I still have some trouble with understanding inheritance-concepts.
What I do not understand is why the following code is not working properly:

Base = function () {
    this.coolVar = "great";
}  

SmallControl = function () {

    // Inheritance:
    this.prototype = new Base();
    this.prototype.constructor = SmallControl;

    this.prototype.init = function(aMap) {
        console.log('init');
        console.log('coolVar?: ' + this.coolVar);
    }
}  
var foo = new SmallControl();  
//foo.init();         // --> TypeError: foo.init is not a function  
foo.prototype.init(); // --> works

If I put the prototype definitions outside of the "SmallControl"-Function everything works fine... but I don't understand that.

Copyright Notice:Content Author:「Roman」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/1205385/inheritance-in-javascript-protoyping-not-in-definition-part

Answers
Quentin 2009-07-30T09:21:43

I think you want something like this:\n\n// Create the super class\nBase = function () {\n this.coolVar = \"great\";\n}; \n\n// Create the new class\nSmallControl = function () {\n}; \n// Set the prototype of SmallControl to be an instance of Base. \n// This runs the Base constructor _immediately_ which sets up the variable\nSmallControl.prototype = new Base();\n// Add the init method to the SmallControl class\nSmallControl.prototype.init = function(aMap) {\n console.log('init');\n console.log('coolVar?: ' + this.coolVar);\n}\n// Create an instance of SmallControl \nvar foo = new SmallControl(); \nfoo.init(); \n",


More about “Inheritance in Javascript - protoyping not in definition-part?” related questions

Inheritance in Javascript - protoyping not in definition-part?

I am currently switching from AS3 to JavaScript. I still have some trouble with understanding inheritance-concepts. What I do not understand is why the following code is not working properly: Base =

Show Detail

Class Inheritance in Javascript

I am wondering how to simulate Class Inheritance in JavaScript. I know class doesn't apply to JavaScript, the way we use is Functions to create objects and do the inheritance things through Prototype

Show Detail

Javascript inheritance

I'm trying a few different approaches to Javascript inheritance at the moment. I have the following code: ('borrowed' from http://www.kevlindev.com/tutorials/javascript/inheritance/index.htm) KV...

Show Detail

JavaScript inheritance

Douglas Crockford seems to like the following inheritance approach: if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; ...

Show Detail

Javascript Privilege Function Inheritance

I have spent the past few weeks doing a ton of research on Javascript inheritance. Firstly, I want to say that I am NOT trying to implement class-based inheritance in Javascript, however, I am tryi...

Show Detail

What is differential inheritance in JavaScript?

This answer on Object.create() method in JavaScript in SO talks about differential inheritance. It goes on to say this : This methods allows you to easily implement differential inheritance, w...

Show Detail

Implement multiple inheritance in Javascript

I have observed that through the use of "prototype" property in Javascript (and indirectly setting up the prototype chain), one can implement multi-level inheritance in JavaScript. But is it possib...

Show Detail

javascript inheritance

I know there is a lot of similar questions are tons of great answers to this. I tried to look at the classical inheritance methods, or those closure methods etc. Somehow I consider they are more or...

Show Detail

Javascript inheritance misbehaviour

I have some js code here link deleted If you open your js console and you'll run this code snipet var r = new TempPopupForm("xxx"); r.create(); an error will appear TypeError: this.init is not a

Show Detail

Javascript inheritance implementation question

In his sitepoint article about javascript inheritance, Harry Fuecks explains a way of implementing inheritance as follows: function copyPrototype(descendant, parent) { var sConstructor = p...

Show Detail