Skip to content

Commit 65e9098

Browse files
authored
Data: Make the data object a regular object again
The change in gh-4603 made the object returned by `elem.data()` a prototype-less object. That's a desired change to support keys colliding with `Object.prototype` properties but it's also a breaking change so it has to wait for jQuery 4.0.0. A 3.x-only test was added to avoid breaking it in the future on this branch. Fixes gh-4665 Ref gh-4603 Closes gh-4666
1 parent 763dd3f commit 65e9098

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/data/Data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Data.prototype = {
2222

2323
// If not, create one
2424
if ( !value ) {
25-
value = Object.create( null );
25+
value = {};
2626

2727
// We can accept data for non-element nodes in modern browsers,
2828
// but we should not, see #8335.

test/unit/data.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -990,17 +990,20 @@ QUnit.test( ".data(prop) does not create expando", function( assert ) {
990990
}
991991
} );
992992

993-
QUnit.test( "keys matching Object.prototype properties (gh-3256)", function( assert ) {
994-
assert.expect( 2 );
993+
QUnit.test( ".data() returns a regular object (jQuery <4 only, gh-4665)", function( assert ) {
994+
assert.expect( 4 );
995995

996-
var div = jQuery( "<div></div>" );
996+
function verifyRegularObject( assert, object ) {
997+
assert.strictEqual( object.hasOwnProperty, Object.prototype.hasOwnProperty,
998+
"Data object has the hasOwnProperty method" );
999+
assert.strictEqual( object + "", "[object Object]",
1000+
"Data object can be stringified" );
1001+
}
9971002

998-
assert.strictEqual( div.data( "hasOwnProperty" ), undefined,
999-
"hasOwnProperty not matched (before forced data creation)" );
1003+
var elem = jQuery( "<div></div>" );
10001004

1001-
// Force the creation of a data object for this element.
1002-
div.data( { foo: "bar" } );
1005+
verifyRegularObject( assert, elem.data() );
10031006

1004-
assert.strictEqual( div.data( "hasOwnProperty" ), undefined,
1005-
"hasOwnProperty not matched (after forced data creation)" );
1007+
elem.data( "foo", "bar" );
1008+
verifyRegularObject( assert, elem.data() );
10061009
} );

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy