What are the downsides of defining functions on prototype this way?

前端 未结 4 624
死守一世寂寞
死守一世寂寞 2020-12-18 10:06

Usually people write code like this:

function Shape() {
  this.x = 0;
  this.y = 0;
}

Shape.prototype.move = function(x, y) {
  this.x += x;
  this.y += y;
         


        
4条回答
  •  伪装坚强ぢ
    2020-12-18 10:27

    If you, or someone in the future trying loop over an object that is extended, the results might be unpredictable and break since the object now contains unknown properties. A little unforeseen gotcha!

提交回复
热议问题