Is there a “right” way to do inheritance in JavaScript? If so, what is it?

后端 未结 5 865
再見小時候
再見小時候 2021-02-02 02:32

I have been trying to learn how to add testing to existing code -- currently reading reading Working Effectively With Legacy Code. I have been trying to apply some of the princ

5条回答
  •  后悔当初
    2021-02-02 03:11

    There are no class in javascript, only objects.
    But if you insist on emulating the class based object-oriented model you can use this:

    function ChildClass() {
        ParentClass.call(this);
        // Write the rest of your constructor function after this point.
    };
    ChildClass.prototype = jQuery.extend({}, ParentClass.prototype, ChildClass.prototype);
    

    jQuery.extend is a 'shallow copy' function from the jQuery library. You can replace it with any other object copying/cloning function.

提交回复
热议问题