garand / sticky

jQuery Plugin for Sticky Objects
Other
3.3k stars 1.06k forks source link

Stop defaults from being overwritten #104

Closed henrahmagix closed 10 years ago

henrahmagix commented 10 years ago

Commit message:

Stop $.extend overwriting defaults object.

When using $.extend, the first argument object is altered, and since objects are copied by reference in Javascript that means the defaults object gets changed when it shouldn't and subsequent calls to $().sticky() will use previous options rather than fresh defaults.

More info:

var defaults = {
  // defaults set in jquery.sticky.js
  topSpacing: 0,
  bottomSpacing: 0,
  className: 'is-sticky',
  wrapperClassName: 'sticky-wrapper',
  center: false,
  getWidthFrom: ''
};
console.log(defaults.center);
// false
var options = {
  // options passed in to $(element).sticky()
  center: true
};
$.extend(defaults, options)
console.log(defaults.center);
// true
// Subsequent sticky calls will be centered.
henrahmagix commented 10 years ago

@garand What do you think?

garand commented 10 years ago

Sounds good. Let's go for it.

henrahmagix commented 10 years ago

Thanks!

garand commented 10 years ago

I've really dropped the ball on maintaining this. :disappointed:

henrahmagix commented 10 years ago

No worries, it happens to the best of us =)