2013-07-21 81 views
6

昨晚,我非常無聊,我想到了一個小小的腳本。基本上我正在考慮PHP與JavaScript相比有多少內置函數,然後我意識到我真的不知道JavaScript實際上有多少函數。我想編寫一個腳本,通過窗口對象來查看對象內部的每個對象等。我寫了腳本,它的工作原理(在較小的對象上嘗試過)。循環瀏覽窗口對象中的所有項目

但是,我的問題是,JavaScript不會讓我循環整個窗口對象。

我曾嘗試:

for (var key in window) { 
    console.log(key); 
} 

我也試過:

var a = Object.create(window); 
for (var key in a) { 
    console.log(key); 
} 

的代碼都片段給我:

top 
window 
location 
external 
chrome 
Intl 
v8Intl 
document 
script1374438467163 
$pick 
$try 
IFrame 
Elements 
OverText 
IframeShim 
Mask 
Clientcide 
dbug 
value 
debugCookie 
StyleWriter 
StickyWin 
TabSwapper 
Collapsible 
Collapsable 
Drag 
Cookie 
Accordion 
Asset 
Spinner 
MultipleOpenAccordion 
MooTools 
typeOf 
instanceOf 
Type 
Hash 
Native 
$A 
$arguments 
$chk 
$clear 
$defined 
$each 
$empty 
$extend 
$H 
$merge 
$lambda 
$mixin 
$random 
$splat 
$time 
$type 
$unlink 
Browser 
$constructor 
Window 
$family 
Document 
$exec 
Slick 
Element 
uniqueNumber 
$ 
getDocument 
getWindow 
Selectors 
$$ 
addListener 
removeListener 
retrieve 
store 
eliminate 
Class 
Chain 
Events 
Options 
Request 
DOMEvent 
Event 
addEvent 
removeEvent 
addEvents 
removeEvents 
fireEvent 
cloneEvents 
Fx 
Swiff 
getSize 
getScroll 
getScrollSize 
getPosition 
getCoordinates 
getHeight 
getWidth 
getScrollTop 
getScrollLeft 
getScrollHeight 
getScrollWidth 
getTop 
getLeft 
setCNETAssetBaseHref 
Table 
BehaviorAPI 
Behavior 
Color 
$RGB 
$HSB 
$HEX 
Keyboard 
Locale 
URI 
CodeMirror 
JSHINT 
_ 
emmet 
Sidebar 
keyMods 
Layout 
MooShellActions 
Base64 
Dropdown 
editorsModified 
Track 
update_resource_input 
remove_resource 
prepareToSubmit 
submit_external_resource 
change_default_input_value 
validate 
warn 
disallowedPlatforms 
default_code_mirror_options 
MooShellEditor 
MooShellSettings 
disqus_developer 
disqus_identifier 
disqus_title 
csspath 
jspath 
imgpath 
mediapath 
codemirrorpath 
panel_html 
panel_css 
panel_js 
makefavouritepath 
example_server 
username 
static_hash 
csrfToken 
mooshell 
preload_resources 
DP 
resources 
default_text 
add_external_resource_url 
_gaq 
TowTruckConfig_enableAnalytics 
TowTruckConfig_cloneClicks 
TowTruck 
_gat 
gaGlobal 
style_html 
css_beautify 
js_beautify 
Beautifier 
language 
Heyoffline 
page_test 
i 
a 
key 
webkitNotifications 
localStorage 
sessionStorage 
applicationCache 
indexedDB 
webkitIndexedDB 
webkitStorageInfo 
CSS 
performance 
console 
devicePixelRatio 
styleMedia 
parent 
opener 
frames 
self 
defaultstatus 
defaultStatus 
status 
name 
length 
closed 
pageYOffset 
pageXOffset 
scrollY 
scrollX 
screenTop 
screenLeft 
screenY 
screenX 
innerWidth 
innerHeight 
outerWidth 
outerHeight 
offscreenBuffering 
frameElement 
crypto 
clientInformation 
navigator 
toolbar 
statusbar 
scrollbars 
personalbar 
menubar 
locationbar 
history 
screen 
postMessage 
close 
blur 
focus 
ondeviceorientation 
ontransitionend 
onwebkittransitionend 
onwebkitanimationstart 
onwebkitanimationiteration 
onwebkitanimationend 
onsearch 
onreset 
onwaiting 
onvolumechange 
onunload 
ontimeupdate 
onsuspend 
onsubmit 
onstorage 
onstalled 
onselect 
onseeking 
onseeked 
onscroll 
onresize 
onratechange 
onprogress 
onpopstate 
onplaying 
onplay 
onpause 
onpageshow 
onpagehide 
ononline 
onoffline 
onmousewheel 
onmouseup 
onmouseover 
onmouseout 
onmousemove 
onmousedown 
onmessage 
onloadstart 
onloadedmetadata 
onloadeddata 
onload 
onkeyup 
onkeypress 
onkeydown 
oninvalid 
oninput 
onhashchange 
onfocus 
onerror 
onended 
onemptied 
ondurationchange 
ondrop 
ondragstart 
ondragover 
ondragleave 
ondragenter 
ondragend 
ondrag 
ondblclick 
oncontextmenu 
onclick 
onchange 
oncanplaythrough 
oncanplay 
onblur 
onbeforeunload 
onabort 
getSelection 
print 
stop 
open 
showModalDialog 
alert 
confirm 
prompt 
find 
scrollBy 
scrollTo 
scroll 
moveBy 
moveTo 
resizeBy 
resizeTo 
matchMedia 
setTimeout 
clearTimeout 
setInterval 
clearInterval 
requestAnimationFrame 
cancelAnimationFrame 
webkitRequestAnimationFrame 
webkitCancelAnimationFrame 
webkitCancelRequestAnimationFrame 
atob 
btoa 
addEventListener 
removeEventListener 
captureEvents 
releaseEvents 
getComputedStyle 
getMatchedCSSRules 
webkitConvertPointFromPageToNode 
webkitConvertPointFromNodeToPage 
dispatchEvent 
webkitRequestFileSystem 
webkitResolveLocalFileSystemURL 
openDatabase 
TEMPORARY 
PERSISTENT 

但是,我知道里面有很多更多的屬性的窗口對象。比如所有的SVG功能和HTML功能。爲什麼JavaScript跳過對象中的許多屬性?

+0

有些屬性是繼承的,有些屬性是不可枚舉的。要獲得不可枚舉的屬性,你可以在'window'和''prototype'鏈中的每個對象上使用'Object.getOwnPropertyNames()'。爲了從原型鏈獲得繼承的對象,可以使用Object.getPrototypeOf()。 http://jsfiddle.net/FtVXN/ –

+0

@CrazyTrain謝謝你,你可以添加,作爲一個答案,如果你想。 – Shawn31313

回答

4

在現代瀏覽器Object.getOwnPropertyNames()Object.getPrototypeOf()將幫助您獲取原型鏈中所有對象的所有屬性。

http://jsfiddle.net/FtVXN/

var obj = window; 

do Object.getOwnPropertyNames(obj).forEach(function(name) { 
     console.log(name); 
    }); 
while(obj = Object.getPrototypeOf(obj)); 

如果你想看到的原型對象的分離,然後添加一行提供了一個分頻器。

http://jsfiddle.net/FtVXN/1/

var obj = window; 

do { 
    Object.getOwnPropertyNames(obj).forEach(function(name) { 
     console.log(name); 
    }); 
    console.log("============================="); 
} while(obj = Object.getPrototypeOf(obj)); 

我認爲我記得在Firefox中,一些全局沒有出現,直到你訪問它們。如果您發現這種情況,您可能需要做一點實驗。

+0

謝謝。這有幫助。 – Shawn31313

+0

不客氣。很高興幫助。 –

相關問題