Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you call parent object functions from child objects in javascript

So, I was reading up on John Resig's blog, saw his micro-templating javascript engine and decided to try and implement my own template management system for javascript, to deepen my understanding of prototype inheritance. However, the minute I started writing it, I ran into a problem.

To start off, here is my base code:

function template_manager() { };

template_manager.prototype = {
    tags: {},
    templates: {},
    output: {},
    default_template: "default",
    set: function (tags, template_name) {
        template_name = "Greetings!";
        //template_name = this._util.template(this.nothing, this.default_template);
        console.log(template_name);
    },
    get: function(tags, template_name) {
        console.log("Getting");
    },
    unset: function(tags, template_name) {
        console.log("Removing");
    },
    render: function(template_name) {
        console.log("Rendering");
    },
    //_util goes here
};

// Take it for a quick test drive.
test = new template_manager;
test.set();
test.get();
test.unset();
test.render();

Then I started working on some common code, and I decided to put it into a utility object:

    _util: {
        // Used to set the default values for optional arguments
        optional: function(obj, def_value) {
            return (typeof obj === "nothing") ? obj : def_value;
        },
        template: function(template_name) {
            return this._util.optional(template_name, this.default_template);
        },
    },

And now, when I try to call my _util.template() function in my set() function I of course get an error because this points to the _util object rather than the template_manager object. I've take a look at the jQuery extend method, and I think that I understand what it's doing. My question is, do I need to implement my own / use jQuery's extend method, or is there another way for me to call the template_manager object from my _util object?

(P. S. I've looked at Douglas Crockford's article on prototype inheritance, and I think the answer is there, but I'm afraid I don't fully understand it yet.)

like image 827
Sean Vieira Avatar asked Mar 03 '10 21:03

Sean Vieira


People also ask

How do you call parent method from child object?

If you override a parent method in its child, child objects will always use the overridden version. But; you can use the keyword super to call the parent method, inside the body of the child method.

How do you call parent functions in child class?

You just have to create an object of the child class and call the function of the parent class using dot(.) operator.

How do you call a parent function from a child in node JS?

constructor = sub; } // parent class function ParentObject(name) { this.name = name; } // parent's methods ParentObject. prototype = { myMethod: function(arg) { this.name = arg; } } // child function ChildObject(name) { // call the parent's constructor ParentObject. call(this, name); this.


1 Answers

You can use call or apply i.e.

template_manager.prototype = {
    set: function (tags, template_name) {
        template_name = "Greetings!";
        template_name = this._util.optional.call(this, this.nothing, this.default_template);
        console.log(template_name);
    }
}

See "Getting Out of Binding Situations in JavaScript" article for more explicit explanation.

like image 70
Li0liQ Avatar answered Sep 28 '22 16:09

Li0liQ