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?
The most straightforward way to loop through an object's properties is by using the for...in statement. This method works in all modern and old browsers including Internet Explorer 6 and higher. Here is an example that uses the for...in loop to iterate over an object:
If you have an array that is considered to be an object in javascript, you can’t loop through the array using map (), forEach (), or a for..of loop. forEach () will give you TypeError: items.forEach is not a function: The most straightforward way to loop through an object's properties is by using the for...in statement.
But the reason for looping over objects depends on the requirement of an application. For example, in your application, you might need the keys of an object, or you want to get all the values without writing all the property names. You can use the following methods to fulfill your requirements in such situations.
My favorite of the three is Object.entries, because you get both the key and property values. Once you’ve converted the object into an array with Object.keys, Object.values, or Object.entries, you can loop through it as if it was a normal array.
In modern browsers Object.getOwnPropertyNames()
and Object.getPrototypeOf()
will help you get all properties of all objects in the prototype chain.
http://jsfiddle.net/FtVXN/
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.
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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With