Created
September 10, 2015 04:02
-
-
Save naosim/11099c63049fc4ebe95b to your computer and use it in GitHub Desktop.
querySelectorAll()の戻り値はArrayじゃないよ ref: http://qiita.com/naosim_/items/f489e200f4e648be2f53
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var divList = document.querySelectorAll('div'); | |
var a = divList[0];// 1つ目の要素 | |
var b = divList[1];// 2つ目の要素 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(divList instanceof Array);// false | |
divList.forEach(function(v, i) { console.log(i); });// エラー |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(toString.call(divList)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mixinArray(target) { | |
var arrayMethods = Object.getOwnPropertyNames(Array.prototype); | |
arrayMethods.forEach(attachArrayMethodsToNodeList); | |
function attachArrayMethodsToNodeList(methodName) { | |
if(methodName !== "length") { | |
target[methodName] = Array.prototype[methodName]; | |
} | |
}; | |
} | |
mixinArray(NodeList.prototype); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mixinArray(divList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment