1

This question already has an answer here:

Looking at the JavaScript below, what does calling the behaviourName do?

  Drupal.behaviors.behaviourName = {
    attach: function (context) {
      $('.someClass', context).once('some-handler').behaviourName(); // what does this do? 
    }
  };

I though that the behaviourName was generally used for name spacing. I can see in the site that im working on that the behaviour name is normally called within itself like in this example. What is this for?

marked as duplicate by Pierre.Vriens, Mołot, Shawn Conn, Krishna Mohan, Yusef Mohamadi Oct 13 '15 at 9:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

You're reading that wrong - the return from once() isn't Drupal.behaviors (it's a collection of jQuery elements), and Drupal.behaviors.behaviourName isn't a function that will do anything by invoking it, anyway. You would need

Drupal.behaviors.behaviourName.attach();

To get anything meaningful, but that's not synonymous with the code you're referencing.

So that code is not calling a javascript behaviour within its own function. It's calling a jQuery extension function on the elements returned from once(). Most commonly when code needs to do what you're describing in Drupal, you'll see Drupal.attachBehaviors(); instead.

What you have is a convention used by the developer who wrote the script, nothing more; you'll need to ask them for their motivation for doing so, and how/where they're attaching the $.fn functions of the same names as the behaviours.

Not the answer you're looking for? Browse other questions tagged or ask your own question.