Skip to content

Setter methods chainable with value=undefined. Fixes #5571.#608

Closed
gibson042 wants to merge 12 commits intojquery:masterfrom
gibson042:ticket_5571
Closed

Setter methods chainable with value=undefined. Fixes #5571.#608
gibson042 wants to merge 12 commits intojquery:masterfrom
gibson042:ticket_5571

Conversation

@gibson042
Copy link
Member

jquery.js: -271 bytes
jquery.min.js: -377 bytes

Includes a reorganization of getter/setter utility jQuery.access to place required parameters at the beginning of the list and allow for bulk operations.

Affected methods:

  • attr
  • prop
  • css
  • data
  • height
  • width
  • text
  • html
  • offset
  • scrollLeft
  • scrollTop
  • queue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the solution is to make a 5 parameter long signature into a 6 parameter signature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both modes of jQuery.access require: collection, key(s), getter/setter function. Setter mode additionally requires a value, plus toggling of what to do when value is a function (use directly or use return value per element) or undefined (no-op or pass to setter). Add in a parameter for telling jQuery.access if it is getting or setting, and that's seven—we're actually saving a parameter by guessing the mode from parameter values.

Maintaining current functionality, the only way I see to reduce the parameter count is by combining setter flags into an options dictionary or bitwise-accessed number.

Alternatively, the method could be streamlined. Would you accept something like this, which abandons the unused functionality of setting values to functions and/or undefined?
return jQuery.access( this, name, jQuery.prop, /*set?*/ arguments.length > 1, value);

Or perhaps this?
return value === undefined && arguments.length > 1 ? this : jQuery.access( this, name, jQuery.prop, value);

src/core.js Outdated
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per feedback from @rwldrn, I have streamlined the parameter signature of jQuery.access, though I am not particularly happy with how it is now called. This touches on a tradeoff between byte count/DRY on one hand and readability/performance on the other.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{Height: "height", Width: "width"} => { Height: "height", Width: "width" } ... but still not sure why this change was made?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To save 17 bytes in jquery.js and 9 in jquery.min.js. You said in comment 18 that you'd strongly oppose adding even a single byte of new code, so I was optimizing pretty aggressively.

@dmethvin
Copy link
Member

dmethvin commented Dec 6, 2011

Landed. 6c2a501

@dmethvin dmethvin closed this Dec 6, 2011
@lock lock bot locked as resolved and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants