Opened 10 years ago
Closed 10 years ago
#33657 closed enhancement (wontfix)
wp.customize.Container.onChangeActive uses "construct" variable name instead of "container"
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 4.3 |
| Component: | Customize | Keywords: | |
| Focuses: | javascript | Cc: |
Description
r32743 introduced the local variable construct into wp.customize.Container.onChangeActive. Other functions in the class (e.g. the initialize method) use container when referring to an object instance. Can we do the same here for consistency?
Attachments (1)
Change History (7)
#1
@
10 years ago
- Summary changed from wp.customize.Container.onChangeActive uses "construct" var instead of "container" to wp.customize.Container.onChangeActive uses "construct" variable name instead of "container"
#3
@
10 years ago
Alternatively, we could use this for using execution context, and consider adding that to the JavaScript coding standards.
I know there's a pattern of using the name of the class as execution context in Customizer JS (e.g. container for api.Container), so there's a preference here I don't want to dismiss outright.
However, I'll make the case for this. this is a native construct to the language, so you can open up a random object or class method and understand what this is with no pretext or research.
Using this would avoid awkward duplication in the case of this ticket:
// this line is awkward focusContainer = container.container.find( 'ul:first' ); // this line reads more clearly focusContainer = this.container.find( 'ul:first' );
For sharing execution context across scopes (e.g. inside a nested callback function), I don't have a particular preference. var self = this is common, but as this is not a native part of the language, everything is convention.
This ticket was mentioned in Slack in #core by eric. View the logs.
10 years ago
#5
@
10 years ago
@ericlewis: There is a case against against using this or self (or that), see #32911:
selfis a bad local variable name. Sometimesselfis a substitute forthiswhen scope is lost in a maze of callbacks, but most of the time, it just makesthismore ambiguous than it already is.
/via @wonderboymusic
I think I wanted to use
constructas opposed tocontainerbecause the latter is often used to represent the DOM container element. So usingconstructwas supposed to be less confusing. See also thefocusfunction:If we changed it from
constructtocontainerthen this would be:container.container