To turn a normal Html select into a select2 the documentation says to simply call select2() on the normal Html select like this
$(document).ready(function() {
$('.js-example-basic-single').select2();
});
When I tried this out, I also imported the select2 module that I installed via npm.
import select2 from 'select2';
What is name of the Javascript feature / concept / technology that allows one to add a new function (in this case .select2()) to an existing object?
Update I forgot to mention that I'm using jquery which is what BJRINT picked up. So it seems this is a jquery + select2 thing.
What is name of the Javascript feature that allows one to add a new function to an existing object?
Prototypes.
Prototypes are the mechanism by which JavaScript objects inherit features from one another. The prototype property can be used to add methods to existing constructors.
For more details about JavaScript prototype, you can refer to MDN documentation on Object prototypes.
$.fn
with a new function called select2
:$.fn.select2 = function (options) {..}
jQuery.fn
is an alias of jQuery.prototype
:jQuery.fn = jQuery.prototype = { ... }
jQuery()
function returns a jQuery object (more details on this).window.jQuery = window.$ = jQuery;
$
is an alias for the jQuery() function$('.js-example-basic-single')
returns a jQuery objectselect2()