昨晚实在无聊,想了个小剧本。基本上我在想 PHP 与 JavaScript 相比有多少内置函数,然后我意识到我真的不知道 JavaScript 实际上有多少函数。我想写一个脚本来查看窗口对象,包括对象内部的每个对象等等。我编写了脚本并且成功了(在较小的对象上进行了尝试)。
但是,我的问题是 JavaScript 不允许我遍历整个 Windows 对象。
我试过:
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
但是,我知道在 windows 对象中还有更多的属性。例如所有的 SVG 函数和 HTML 函数。为什么 JavaScript 会跳过对象中的许多属性?
最佳答案
在现代浏览器中,Object.getOwnPropertyNames() 和 Object.getPrototypeOf() 将帮助您获取原型(prototype)链中所有对象的所有属性。
var obj = window;
do Object.getOwnPropertyNames(obj).forEach(function(name) {
console.log(name);
});
while(obj = Object.getPrototypeOf(obj));
如果你想看到原型(prototype)对象的分离,那么添加一条提供分隔线的线。
var obj = window;
do {
Object.getOwnPropertyNames(obj).forEach(function(name) {
console.log(name);
});
console.log("=============================");
} while(obj = Object.getPrototypeOf(obj));
我确实记得在 Firefox 中,有些全局变量只有在您访问它们时才会出现。如果您发现情况确实如此,您可能需要进行一些试验。
关于javascript - 遍历窗口对象中的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17776830/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer