The Benefits of JavaScript Prototype

前端 未结 2 1222
情话喂你
情话喂你 2020-12-14 12:27

I\'ve been wondering about JavaScript\'s prototypal nature, and the benefits of it, and have come down to the following list :

1) Inheritance

相关标签:
2条回答
  • 2020-12-14 12:43

    Those are all correct.

    Of course, there are "drawbacks" as well:

    No closures

    function a() {
        var ival = 0;
        this.start = function(){ ival = setInterval(function(){ }, 300); }
        this.finish = function(){ clearTimeout(ival); }
    }
    

    compare to:

    function a() {
        this.ival = 0;
    }
    a.prototype.start = function(){ this.ival = setInterval(function(){ }, 300); }
    a.prototype.finish = function(){ clearTimeout(this.ival); }
    
    0 讨论(0)
  • 2020-12-14 13:01

    http://en.wikipedia.org/wiki/Prototype-based_programming#Comparison_with_class-based_models

    Also, please see the discussion of prototype inheritance in the answers to this:

    prototype based vs. class based inheritance

    0 讨论(0)
提交回复
热议问题