Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Question) Unclear $.fn initiation #16

Closed
ametad opened this issue May 22, 2015 · 4 comments
Closed

(Question) Unclear $.fn initiation #16

ametad opened this issue May 22, 2015 · 4 comments

Comments

@ametad
Copy link

ametad commented May 22, 2015

Hi Bojan,

Nice plugin you build! I was searching for a good example of the pattern you are using and so I found Tipso.

But this line is not clear for me: https://github.com/object505/tipso/blob/master/src/tipso.js#L504 (line number changes)

What is the meaning of the following?

if (!(this instanceof $)) {
    $.extend(defaults, options);
}
@object505
Copy link
Owner

Hello,
That is used to set the default options globally for all instances of the plugin.
Here is a jsfiddle demonstration of it.
http://jsfiddle.net/object505/1kb064w7/1/

Let me know if this is helpful :)

@ametad
Copy link
Author

ametad commented May 25, 2015

Thank you for your response. It is helpful, yes :) But.. I thought that the custom override of defaults is done here: https://github.com/object505/tipso/blob/master/src/tipso.js#L33 (line number changes)

  function Plugin(element, options) {
    this.element = $(element);
    this.doc = $(document);
    this.win = $(window);
    this.settings = $.extend({}, defaults, options);
    this._defaults = defaults;
    this._name = pluginName;
    this._title = this.element.attr('title');
    this.mode = 'hide';
    this.ieFade = !supportsTransitions;

    this.init();
  }

Isn't it?
The specific code at line 504 does check if 'this' is part of jQuery.. right? Or perhaps is this the way you do not have to chain the initiation at an element?

$.tipso({
    background: "#000"
});

// NOT necessary 
$(#id).tipso({
    background: "#000"
});

It is just that I don't understand and want to learn what is happening.. Thank you for your time!

@object505
Copy link
Owner

It is like this,
When we have this part
$.extend({}, defaults, options);
we are saying to merge the defaults and options, without modifying the defaults.

And in the second part
if (!(this instanceof $)) { $.extend(defaults, options); }
we say if this is not a jQuery object ( that is why we call it like this $.tipso() ) then merge the defaults and options, modifying the defaults so the new options will be defaults of the plugin.

Hopefully this would clear things more :)

@ametad
Copy link
Author

ametad commented May 28, 2015

Thank you very much! This is helpful. Keep up 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants