如何在javascript中从子对象调用父对象函数

发布于 2024-08-23 21:37:06 字数 2000 浏览 3 评论 0原文

所以,我在 John Resig 的博客上阅读,看到了他的微型模板化 javascript 引擎 并决定尝试实现自己的 javascript 模板管理系统,以加深对原型继承的理解。然而,当我开始写它的那一刻,我遇到了一个问题。

首先,这是我的基本代码:

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();

然后我开始处理一些通用代码,并决定将其放入实用程序对象中:

    _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);
        },
    },

现在,当我尝试调用我的 _util.template() 在我的 set() 函数中,我当然会得到一个错误,因为 this 指向 _util 对象而不是 template_manager< /代码> 对象。我查看了 jQuery extend 方法,我想我明白它在做什么。我的问题是,我是否需要实现自己的/使用 jQuery 的 extend 方法,或者是否有其他方法可以调用 template_manager 对象来自我的 _util 对象?

(PS我看过Douglas Crockford关于原型继承的文章,我认为 答案就在那里,但恐怕我还没有完全理解。)

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶情人眼里出诗心の 2024-08-30 21:37:06

您可以使用 callapply

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);
    }
}

参见"获取JavaScript 中的脱离绑定情况” 文章以获取更明确的解释。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文