Last night, I got really bored and I thought of a little idea for a little script. Basically I was thinking about how many built-in functions PHP has compared to JavaScript and then I realized that I really don't know how many functions JavaScript actually has. I thought of writing a script that would look through the window object including every object inside the object and so forth. I wrote the script and it worked (tried it on a smaller object).
However, my problem is that JavaScript wont let me loop though the whole windows object.
I have tried:
for (var key in window) {
console.log(key);
}
I also tried:
var a = Object.create(window);
for (var key in a) {
console.log(key);
}
Both snippets of code give me:
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
However, I know there are plenty more properties inside of the windows object. Such as all of the SVG functions and HTML functions. Why is JavaScript skipping many of the properties in the object?
In modern browsers Object.getOwnPropertyNames()
and Object.getPrototypeOf()
will help you get all properties of all objects in the prototype chain.
var obj = window;
do Object.getOwnPropertyNames(obj).forEach(function(name) {
console.log(name);
});
while(obj = Object.getPrototypeOf(obj));
If you want to see the separation of the prototype objects, then add a line that provides a divider.
var obj = window;
do {
Object.getOwnPropertyNames(obj).forEach(function(name) {
console.log(name);
});
console.log("=============================");
} while(obj = Object.getPrototypeOf(obj));
I do think I recall that in Firefox, some globals don't appear until you access them. You may need to do a little experimenting if you find that to be the case.
来源:https://stackoverflow.com/questions/17776830/looping-through-all-of-the-items-in-the-window-object