Using super methods in Javascript based on Crockford's functional inheritance

后端 未结 2 1189
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 16:36

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 s

2条回答
  •  滥情空心
    2021-01-13 17:25

    The idea here is that this:

    var super_get_name = that.superior('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.

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

提交回复
热议问题