2

I'm a beginner with JavaScript Objects and Prototypes and trying to develop my first " multi-level inherited" JS Objects, an unexpected issue came up. This is my code:

var Utils = function () {};
Utils.prototype = {
    sayHelloGeneral: function(){
        console.log('hello');
    }
};

var FormTools = function () {
    Utils.call(this);
    this.fields = [];
};
FormTools.prototype = Object.create(Utils.prototype);
FormTools.prototype.constructor = FormTools;
FormTools.prototype.sayHelloForm= function (fields) {
    console.log('hello form');
};

function GroupManager(value) {
    FormTools.call(this);

    this.val = typeof values === 'undefined' ? 1 : value;
};
GroupManager.prototype = Object.create(FormTools.prototype);
GroupManager.prototype.constructor = GroupManager;
GroupManager.prototype.helloGroupManager= function (givenValue) {
    console.log('Hello group manager');
};

Why when I try to call the group manager, it prints only the sayHelloGeneral function?

var GM = new GroupManager;

GM.sayHelloGeneral(); //->ok
GM.helloGroupManager(); //--> ok
GM.sayHelloForm(); //->sayHelloForm is not a function
3
  • jsfiddle.net/gvhmfoux Commented Oct 9, 2015 at 10:26
  • Working fine for me..where is your sayhello()? Commented Oct 9, 2015 at 10:27
  • 1
    Sorry it was sayHelloGeneral. Fixed :) Commented Oct 9, 2015 at 10:31

1 Answer 1

1

It seems to be working fine. See the snippet below

var Utils = function () {};
Utils.prototype = {
    sayHelloGeneral: function(){
        console.log('hello');
    }
};

var FormTools = function () {
    Utils.call(this);
    this.fields = [];
};
FormTools.prototype = Object.create(Utils.prototype);
FormTools.prototype.constructor = FormTools;
FormTools.prototype.sayHelloForm= function (fields) {
    console.log('hello form');
};

function GroupManager(value) {
    FormTools.call(this);

    this.val = typeof values === 'undefined' ? 1 : value;
};
GroupManager.prototype = Object.create(FormTools.prototype);
GroupManager.prototype.constructor = GroupManager;
GroupManager.prototype.helloGroupManager= function (givenValue) {
    console.log('Hello group manager');
};


var GM = new GroupManager;

//GM.sayhello(); //->ok---> should be sayHelloGeneral()
GM.sayHelloGeneral();
GM.helloGroupManager(); //--> ok
GM.sayHelloForm(); //->Works fine too

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks guys! I realised that the problem was in my code FormTools.prototype.sayHelloForm was FormTools.prototypesayHelloForm. Of course above it's just an example and my code is far bigger than my silly example :) Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.