mirror of
https://github.com/jquery/jquery.git
synced 2026-04-20 03:01:22 -04:00
Core: Make jQuery objects iterable
Make iterating over jQuery objects possible using ES 2015 for-of:
for ( node of $( "<div id=narwhal>" ) ) {
console.log( node.id ); // "narwhal"
}
Fixes gh-1693
This commit is contained in:
8
test/node_smoke_tests/iterable_with_native_symbol.js
Normal file
8
test/node_smoke_tests/iterable_with_native_symbol.js
Normal file
@@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
if ( typeof Symbol === "undefined" ) {
|
||||
console.log( "Symbols not supported, skipping the test..." );
|
||||
process.exit();
|
||||
}
|
||||
|
||||
require( "./lib/ensure_iterability_es6" )();
|
||||
13
test/node_smoke_tests/iterable_with_symbol_polyfill.js
Normal file
13
test/node_smoke_tests/iterable_with_symbol_polyfill.js
Normal file
@@ -0,0 +1,13 @@
|
||||
/* jshint esnext: true */
|
||||
|
||||
"use strict";
|
||||
|
||||
var assert = require( "assert" );
|
||||
|
||||
delete global.Symbol;
|
||||
require( "core-js" );
|
||||
|
||||
assert.strictEqual( typeof Symbol, "function", "Expected Symbol to be a function" );
|
||||
assert.notEqual( typeof Symbol.iterator, "symbol", "Expected Symbol.iterator to be polyfilled" );
|
||||
|
||||
require( "./lib/ensure_iterability" )();
|
||||
25
test/node_smoke_tests/lib/ensure_iterability_es6.js
Normal file
25
test/node_smoke_tests/lib/ensure_iterability_es6.js
Normal file
@@ -0,0 +1,25 @@
|
||||
/* jshint esnext: true */
|
||||
|
||||
"use strict";
|
||||
|
||||
var assert = require( "assert" );
|
||||
|
||||
module.exports = function ensureIterability() {
|
||||
require( "jsdom" ).env( "", function( errors, window ) {
|
||||
assert.ifError( errors );
|
||||
|
||||
var i,
|
||||
ensureJQuery = require( "./ensure_jquery" ),
|
||||
jQuery = require( "../../../dist/jquery.js" )( window ),
|
||||
elem = jQuery( "<div></div><span></span><a></a>" ),
|
||||
result = "";
|
||||
|
||||
ensureJQuery( jQuery );
|
||||
|
||||
for ( i of elem ) {
|
||||
result += i.nodeName;
|
||||
}
|
||||
|
||||
assert.strictEqual( result, "DIVSPANA", "for-of doesn't work on jQuery objects" );
|
||||
} );
|
||||
};
|
||||
Reference in New Issue
Block a user