Skip to content

Commit ed637b0

Browse files
authoredNov 18, 2021
Widget: Make contextless widget construction work
Due to the fact the widget factory code is now in strict mode, the check for being called without using the `new` keyword started breaking if you save the widget constructor to a variable before calling it: ```js var customWidget = $.custom.customWidget; customWidget( {}, elem ); ``` as then `this` is undefined and checking for `this._createWidget` crashes. Account for that with an additional check. Fixes gh-2015 Closes gh-2019
1 parent b52ee40 commit ed637b0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed
 

‎tests/unit/widget/core.js

+12
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ QUnit.test( "element normalization", function( assert ) {
9292
$.ui.testWidget();
9393
} );
9494

95+
QUnit.test( "contextless construction", function( assert ) {
96+
assert.expect( 1 );
97+
var testWidget,
98+
elem = $( "<div>" );
99+
100+
$.widget( "ui.testWidget", {} );
101+
testWidget = $.ui.testWidget;
102+
103+
testWidget( {}, elem );
104+
assert.ok( true, "No crash" );
105+
} );
106+
95107
QUnit.test( "custom selector expression", function( assert ) {
96108
assert.expect( 1 );
97109
var elem = $( "<div>" ).appendTo( "#qunit-fixture" );

‎ui/widget.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ $.widget = function( name, base, prototype ) {
7777
constructor = $[ namespace ][ name ] = function( options, element ) {
7878

7979
// Allow instantiation without "new" keyword
80-
if ( !this._createWidget ) {
80+
if ( !this || !this._createWidget ) {
8181
return new constructor( options, element );
8282
}
8383

0 commit comments

Comments
 (0)
Please sign in to comment.