In your test with .some-element
you would get an empty jQuery object if .some-element
matches nothing since $.fn.plugin
returns what this.each(...)
returns and the jQuery.each
function returns exactly the same jQuery
object as what it was called on.
As to how to get the instance, let's go through each step:
var plugin = $('.element').plugin();
The line above won't create a plugin if $('.element')
matches nothing. One thing for sure, the value you assign to plugin
is equal to $('.element')
.
var instance = $('.element').data('plugin',plugin);
In context, the line above is equivalent to var instance = $('.element').data('plugin', $('.element'));
And the return value of jQuery.data(key, value)
is the jQuery object itself, so that here instance
will be equal to $('.element')
.
console.log(instance); // an empty object again!
That's what you'd get if $('.element')
matches nothing.
console.log(instance.someMethod("hello world"));
This is definitely not going to work.
How to access the plugin instance is detailed in a comment in your code:
// var instance = $('.element').data('plugin');
// instance.someMethod();
Of course for this to work, $('.element')
has to match a non-empty set of elements.
AMD is not a factor at all in your problems. All of the issues here have to do with how to use jQuery and how to write jQuery plugins.