Drupal.behaviors, attach and origin of it The structure of Drupal.behaviors is rather similar in Drupal 7 and Drupal 8. Where do attach() and detach() come from?
Thid code is from drupal.js , still wondering where attach method in this.attach is defined?

Drupal.attachBehaviors = function (context, settings) {
  context = context || document;
  settings = settings || Drupal.settings;
  // Execute all of them.
  $.each(Drupal.behaviors, function () {
    if ($.isFunction(this.attach)) {
      this.attach(context, settings);
    }
  });
};
The attach() and detach() methods are part of the Behaviors interface defined by Drupal:


  Any object defined as a property of Drupal.behaviors will get its
  attach() method called when the DOM has loaded both initially and
  after any AJAX calls.


https://www.drupal.org/docs/8/api/javascript-api/javascript-api-overview