在基于Crockford的功能继承的Javascript中使用超级方法 [英] Using super methods in Javascript based on Crockford's functional inheritance

查看:102
本文介绍了在基于Crockford的功能继承的Javascript中使用超级方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读Crockford的'The Good Parts'中关于功能继承的章节。在哺乳动物的例子中,他让我有点困惑,为什么他使用上级方法来修改 get_name 功能。以下是有问题的示例:

I've been reading the chapter on functional inheritance in Crockford's 'The Good Parts'. In the mammal example he gives I'm a bit confused as to why he uses the superior method to modify the get_name function. Here is the example in question:

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

var mammal = function (spec) {
    var that = {};

    that.get_name = function () {
        return spec.name;
    };

    that.says = function () {
        return spec.saying || '';
    };

    return that;
};

var myMammal = mammal({
    name: 'Herb'
});

var cat = function (spec) {
    spec.saying = spec.saying || 'meow';
    var that = mammal(spec);

    that.purr = function (n) {
        var i, s = '';
        for (i = 0; i < n; i += 1) {
            if (s) {
                s += '-';
            }
            s += 'r';
        }
        return s;
    };

    that.get_name = function () {
        return that.says() + ' ' + spec.name + ' ' + that.says();
    };

    return that;
};

Object.method('superior', function (name) {
    var that = this,
        method = that[name];
    return function () {
        return method.apply(that, arguments);
    };
});

var coolcat = function (spec) {
    var that = cat(spec);
    var super_get_name = that.superior('get_name');

    that.get_name = function (n) {
        return 'like ' + super_get_name() + ' baby';
    };
    return that;
};

var myCoolCat = coolcat({
    name: 'Bix'
});

var name = myCoolCat.get_name(); // 'like meow Bix meow baby'

我对此很困惑因为我可以复制同样的通过删除上级方法并只更改 coolcat ,如下所示:

I'm confused about this because I can replicate the same thing by removing the superior method and just changing coolcat as follows:

var coolcat = function(spec) {
    var that = cat(spec);
    var super_get_name = that.get_name();

    that.get_name = function(n) {
        return 'like ' + super_get_name + ' baby';
    };
    return that;
};

所以,我不明白为什么Crockford选择使用 superior 方法。是否有人能够解释?

So, I don't understand why Crockford chooses to use the superior method. Is anyone able to explain at all?

推荐答案

这里的想法是:

var super_get_name = that.superior('get_name');

super_get_name 转换为—每次被称为—调用 原始 get_name 方法。这允许 new get_name 调用(超类) get_name

makes super_get_name into a function that — every time it is called — invokes that's original get_name method. This allows the new get_name to call the old (super-class) get_name.

现在,如果原始的 get_name 方法除了返回之外永远不会有任何影响一个永不改变的单一价值,是的,这是毫无意义的;你可以保存那个永不改变的单值,然后在新的 get_name 中使用它。但是如果原始的 get_name 实际上可以做某事(比如说,运行一个AJAX请求,或者改变HTML元素的样式),或者它的返回值是否可以改变(比方说,如果有一些相应的 set_name 方法),那么你的代码之间会有一个重要的区别(保存原始的返回值并使用它)和Crockford的代码做了什么(保存原始方法并调用它)。

Now, if the original get_name method will never have any effect other than to return a single value that never changes, then yeah, this is kind of pointless; you can just save that single-value-that-never-changes and then use it in the new get_name. But if the original get_name can actually do things (such as, say, run an AJAX request, or change the styling of an HTML element), or if its return-value can change (say, if there were some corresponding set_name method), then there would be an important difference between what your code does (save the original return-value and use it) and what Crockford's code does (save the original method and invoke it).

这篇关于在基于Crockford的功能继承的Javascript中使用超级方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆