How i extends class in p5.js?

JavaScript got a prototype inheritance system, in which we make available all properties to be extended by annexing them all to a constructor function’s property object {} called prototype: :card_file_box:

For example If we wanna extend the class p5 (classes are constructor functions btW) w/ a new method called square(), we can simply annex it to p5.prototype{} object, like this: :+1:

p5.prototype.square = function (x, y, size) {
  return this.rect(x, y, size, size);
};

And a more complex example, but now extending the class p5.Image instead, w/ a new method called resizeNN(): :ok_hand:

1 Like