1

我不确定为什么这会输出对象undefined的属性名称document。我认为这与变量表达式有关myArray[i++]。我可以把它变成一个简单的原始变量,我会得到所有的属性名,但我想把所有的属性名保存到一个数组中。提前谢谢你的帮助!!

var myArray = [];
var i = 0;
for (myArray[i++] in document) {
document.write(myArray[i] + "</br>");
}
4

4 回答 4

4

它不起作用,因为您i在设置后myArray[i]通过执行myArray[i++](后递增)递增。

这会起作用,尽管我不确定你为什么需要这样做。

var myArray = [];
var i = 0;
for (myArray[i] in document) {
    console.log(myArray[i++] + "</br>");
}
于 2013-04-26T00:08:59.187 回答
4

发生这种情况是因为您总是在数组中查看比您定义的更远的一个元素,因为您 post-increment i。您可以改为预先增加:

var myArray = [];
var i = -1;
for (myArray[++i] in document) {
    document.write(myArray[i] + "</br>");
}

JSFiddle

于 2013-04-26T00:09:03.900 回答
1

“我希望将所有属性名称保存到一个数组中。”

你可以这样做:

var names = Object.getOwnPropertyNames(document);

原因Object.getOwnPropertyNames()是它包含不可枚举的属性,而for-in将跳过它们。

但是有些属性是继承的,所以如果你想要的话,你需要对原型链中的所有对象做同样的事情。

var obj = document;
var names = [];

do {
    names.push.apply(names, Object.getOwnPropertyNames(obj));
} while(obj = Object.getPrototypeOf(obj));

结果在 Firefox 中:

[“jQuery17104169501299363202”,“位置”,“addEventListener”,“removeEventListener”,“dispatchEvent”,“getElementsByName”,“getItems”,“open”,“close”,“write”,“writeln”,“execCommand”,“ queryCommandEnabled”、“queryCommandIndeterm”、“queryCommandState”、“queryCommandSupported”、“queryCommandValue”、“clear”、“getSelection”、“captureEvents”、“releaseEvents”、“routeEvent”、“domain”、“cookie”、“body” , “head”, “images”, “embeds”, “plugins”, “links”, “forms”, “scripts”, “designMode”, “fgColor”, “linkColor”,“vlinkColor”、“alinkColor”、“bgColor”、“锚点”、“小程序”、“onreadystatechange”、“onmouseenter”、“onmouseleave”、“getElementsByTagName”、“getElementsByTagNameNS”、“getElementsByClassName”、“getElementById”、“createElement” "、"createElementNS"、"createDocumentFragment"、"createTextNode"、"createComment"、"createProcessingInstruction"、"importNode"、"adoptNode"、"createEvent"、"createRange"、"createNodeIterator"、"createTreeWalker"、"createCDATASection"、 “createAttribute”、“createAttributeNS”、“hasFocus”、“releaseCapture”、“mozSetImageElement”、“mozCancelFullScreen、“mozExitPointerLock”、“enableStyleSheetsForSet”、“elementFromPoint”、“querySelector”、“querySelectorAll”、“getAnonymousNodes”、“getAnonymousElementByAttribute”、“addBinding”、“removeBinding”、“getBindingParent”、“loadBindingDocument”、“createExpression” 、“createNSResolver”、“评估”、“实现”、“URL”、“documentURI”、“compatMode”、“characterSet”、“contentType”、“doctype”、“documentElement”、“inputEncoding”、“referrer”、“ lastModified”、“readyState”、“title”、“dir”、“defaultView”、“activeElement”、“currentScript”、“mozFullScreenEnabled”、“mozFullScreenElement”、“mozFullScreen”、“mozPointerLockElement”、“hidden”、“mozHidden”、“visibilityState”、“mozVisibilityState”、“styleSheets”、“selectedStyleSheetSet”、“lastStyleSheetSet”、“preferredStyleSheetSet” 、“styleSheetSets”、“hasChildNodes”、“insertBefore”、“appendChild”、“replaceChild”、“removeChild”、“normalize”、“cloneNode”、“isEqualNode”、“compareDocumentPosition”、“contains”、“lookupPrefix”、“ lookupNamespaceURI”、“isDefaultNamespace”、“isSupported”、“hasAttributes”、“setUserData”、“getUserData”、“nodeType”、“nodeName”、“baseURI”、“ownerDocument”、“parentNode”、“parentElement”、“childNodes”、“firstChild”、“lastChild”、“previousSibling”、“nextSibling”、“nodeValue”、“textContent ", "属性", "namespaceURI", "前缀", "localName", "lookupGetter ”、“ lookupSetter ”、“ defineGetter ”、“ defineSetter ”"、"QueryInterface"、"mozSyntheticDocument"、"caretPositionFromPoint"、"ELEMENT_NODE"、"ATTRIBUTE_NODE"、"TEXT_NODE"、"CDATA_SECTION_NODE"、"ENTITY_REFERENCE_NODE"、"ENTITY_NODE"、"PROCESSING_INSTRUCTION_NODE"、"COMMENT_NODE"、"DOCUMENT_NODE"、 “ document_type_node”,“ document_fragment_node”,“ notation_node”,“ document_position_disconnected”,“ document_position_position_preceding”,“ document_position_following”,“ document_position_position_contains”,“ ", "oncanplaythrough", "onchange”、“onclick”、“oncontextmenu”、“ondblclick”、“ondrag”、“ondragend”、“ondragenter”、“ondragleave”、“ondragover”、“ondragstart”、“ondrop”、“ondurationchange”、“onemptied” , “onended”, “onerror”, “onfocus”, “oninput”, “oninvalid”, “onkeydown”, “onkeypress”, “onkeyup”, “onload”, “onloadeddata”, “onloadedmetadata”, “onloadstart”, “ onmousedown”、“onmousemove”、“onmouseout”、“onmouseover”、“onmouseup”、“onmozfullscreenchange”、“onmozfullscreenerror”、“onmozpointerlockchange”、“onmozpointerlockerror”、“onpause”,“onplay”,“onplaying”,“onprogress”,“onratechange”,“onreset”,“onscroll”,“onseed”,“onseeing”,“onselect”,“onshow”,“onstalled”,“onsubmit”,“ onsuspend”、“ontimeupdate”、“onvolumechange”、“onwaiting”、“onwheel”、“oncopy”、“oncut”、“onpaste”、“onbeforescriptexecute”、“onafterscriptexecute”、“getElementsByTagName”、“getElementsByTagNameNS”、“getElementsByClassName” 、“getElementById”、“createElement”、“createElementNS”、“createDocumentFragment”、“createTextNode”、“createComment”、“createProcessingInstruction”、“importNode”、“adoptNode”、“createEvent”、“createRange”、“createNodeIterator”、“createTreeWalker”、“createCDATASection”、“createAttribute”、“createAttributeNS”、“hasFocus”、“releaseCapture”、“mozSetImageElement”、“mozCancelFullScreen” ,“mozExitPointerLock”,“enableStyleSheetsForSet”,“elementFromPoint”,“querySelector”,“querySelectorAll”,“getAnonymousNodes”,“getAnonymousElementByAttribute”,“addBinding”,“removeBinding”,“getBindingParent”,“loadBindingDocument”,“createExpression”,“ createNSResolver”,“评估”,“实现”,“URL”,“documentURI”,“compatMode”、“characterSet”、“contentType”、“doctype”、“documentElement”、“inputEncoding”、“referrer”、“lastModified”、“readyState”、“title”、“dir”、“defaultView”、“activeElement” ","onabort","onblur","oncanplay","oncanplaythrough","onchange","onclick","oncontextmenu","ondblclick","ondrag","ondragend","ondragenter","ondragleave", “ondragover”、“ondragstart”、“ondrop”、“ondurationchange”、“onemptied”、“onended”、“onerror”、“onfocus”、“oninput”、“oninvalid”、“onkeydown”、“onkeypress”、“onkeyup”、“onload”、“onloadeddata”、“onloadedmetadata”、“onloadstart”、“onmousedown”、“onmousemove”、“onmouseout”、“onmouseover”、“onmouseup”、“onpause”、“onplay” , “onplaying”, “onprogress”, “onratechange”, “onreset”, “onscroll”, “onseeed”, “onseeking”, “onselect”, “onshow”, “onstalled”, “onsubmit”, “onsuspend”, “ ontimeupdate”、“onvolumechange”、“onwaiting”、“onreadystatechange”、“onmouseenter”、“onmouseleave”、“onmozfullscreenchange”、“onmozfullscreenerror”、“onmozpointerlockchange”、“onmozpointerlockerror”、“onwheel”、“oncopy”、“oncut”、“onpaste”、“onbeforescriptexecute”、“onafterscriptexecute”、“currentScript”、“mozFullScreenEnabled”、“mozFullScreenElement”、“mozFullScreen”、“mozPointerLockElement”、“隐藏” 、“mozHidden”、“visibilityState”、“mozVisibilityState”、“styleSheets”、“selectedStyleSheetSet”、“lastStyleSheetSet”、“preferredStyleSheetSet”、“styleSheetSets”、“构造函数”、“hasChildNodes”、“insertBefore”、“appendChild”、“ replaceChild”、“removeChild”、“规范化”、“cloneNode”、“isEqualNode”、“compareDocumentPosition”、“包含”、“lookupPrefix”、“lookupNamespaceURI”、“isDefaultNamespace”、“isSupported”、“hasAttributes”、“setUserData”、“getUserData”、“nodeType”、“nodeName”、“baseURI”、“ownerDocument”、“parentNode” "、"parentElement"、"childNodes"、"firstChild"、"lastChild"、"previousSibling"、"nextSibling"、"nodeValue"、"textContent"、"attributes"、"namespaceURI"、"prefix"、"localName"、 “ELEMENT_NODE”、“ATTRIBUTE_NODE”、“TEXT_NODE”、“CDATA_SECTION_NODE”、“ENTITY_REFERENCE_NODE”、“ENTITY_NODE”、“PROCESSING_INSTRUCTION_NODE”、“COMMENT_NODE”、“ document_node”,“ document_type_node”,“ document_fragment_node”,“ notation_node”,“ document_position_position_position_position_disconnected”,“ document_position_position_preceding”,“ document_position_following”,“ ”、“dispatchEvent”、“构造函数”、“构造函数”、“toSource”、“toString”、“toLocaleString”、“valueOf”、“watch”、“unwatch”、“hasOwnProperty”、“isPrototypeOf”、“propertyIsEnumerable”、 "document_fragment_node”,“ notation_node”,“ document_position_disconnected”,“ document_position_preceding”,“ document_position_position_following”,“ document_position_contains”,“ document_position_position_contained_by”,“ ,“构造函数”,“toSource”,“toString”,“toLocaleString”,“valueOf”,“watch”,“unwatch”,“hasOwnProperty”,“isPrototypeOf”,“propertyIsEnumerable”,“document_fragment_node”,“ notation_node”,“ document_position_disconnected”,“ document_position_preceding”,“ document_position_position_following”,“ document_position_contains”,“ document_position_position_contained_by”,“ ,“构造函数”,“toSource”,“toString”,“toLocaleString”,“valueOf”,“watch”,“unwatch”,“hasOwnProperty”,“isPrototypeOf”,“propertyIsEnumerable”,“DOCUMENT_POSITION_FOLLOWING”、“DOCUMENT_POSITION_CONTAINS”、“DOCUMENT_POSITION_CONTAINED_BY”、“DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC”、“构造函数”、“addEventListener”、“removeEventListener”、“dispatchEvent”、“构造函数”、“构造函数”、“toSource”、“toString”、“toLocaleString” 、“valueOf”、“watch”、“unwatch”、“hasOwnProperty”、“isPrototypeOf”、“propertyIsEnumerable”、“DOCUMENT_POSITION_FOLLOWING”、“DOCUMENT_POSITION_CONTAINS”、“DOCUMENT_POSITION_CONTAINED_BY”、“DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC”、“构造函数”、“addEventListener”、“removeEventListener”、“dispatchEvent”、“构造函数”、“构造函数”、“toSource”、“toString”、“toLocaleString” 、“valueOf”、“watch”、“unwatch”、“hasOwnProperty”、“isPrototypeOf”、“propertyIsEnumerable”、“构造函数”、“构造函数”、“toSource”、“toString”、“toLocaleString”、“valueOf”、“watch”、“unwatch”、“hasOwnProperty”、“isPrototypeOf”、“propertyIsEnumerable”、“构造函数”、“构造函数”、“toSource”、“toString”、“toLocaleString”、“valueOf”、“watch”、“unwatch”、“hasOwnProperty”、“isPrototypeOf”、“propertyIsEnumerable”、“defineGetter ”、“ defineSetter ”、“ lookupGetter ”、“ lookupSetter ”]

于 2013-04-26T00:06:21.413 回答
1

我认为您可能想要使用如下所示的循环:

for(int i = 0; i < myArray.length; i++){
  document.write(myArray[i] + "</br>");
}
于 2013-04-26T00:08:28.133 回答