Skip to content
This repository has been archived by the owner on Sep 11, 2022. It is now read-only.

Stop defaults from being overwritten #104

Merged
merged 1 commit into from Jun 25, 2014

Conversation

henrahmagix
Copy link
Contributor

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.

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.
@henrahmagix
Copy link
Contributor Author

@garand What do you think?

@garand
Copy link
Owner

garand commented Jun 25, 2014

Sounds good. Let's go for it.

garand pushed a commit that referenced this pull request Jun 25, 2014
Stop defaults from being overwritten
@garand garand merged commit ec5e9ee into garand:master Jun 25, 2014
@henrahmagix
Copy link
Contributor Author

Thanks!

@henrahmagix henrahmagix deleted the stop-overwriting-defaults branch June 25, 2014 15:55
@garand
Copy link
Owner

garand commented Jun 25, 2014

I've really dropped the ball on maintaining this. 😞

@henrahmagix
Copy link
Contributor Author

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

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

Successfully merging this pull request may close these issues.

None yet

2 participants