Skip to content

Instantly share code, notes, and snippets.

@naosim
Created September 10, 2015 04:02
Show Gist options
  • Save naosim/11099c63049fc4ebe95b to your computer and use it in GitHub Desktop.
Save naosim/11099c63049fc4ebe95b to your computer and use it in GitHub Desktop.
querySelectorAll()の戻り値はArrayじゃないよ ref: http://qiita.com/naosim_/items/f489e200f4e648be2f53
var divList = document.querySelectorAll('div');
var a = divList[0];// 1つ目の要素
var b = divList[1];// 2つ目の要素
console.log(divList instanceof Array);// false
divList.forEach(function(v, i) { console.log(i); });// エラー
console.log(toString.call(divList));
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);
mixinArray(divList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment