javascript是否允许别名评估?以下代码的第一部分意外行为(显示1,1),但第二部分不行(显示1,2).对ECMA脚本或mozilla文档的引用将有所帮助,我找不到一个.
<html>
<script type="application/javascript;version=1.8">
(function(){
eval('var testVar=1');
alert(testVar);
var eval2=eval;
eval2('var testVar=2');
alert(testVar);
})();
(function(){
eval('var testVar=1');
alert(testVar);
eval('var testVar=2');
alert(testVar);
})();
</script>
</html>
Run Code Online (Sandbox Code Playgroud) 为什么下面的代码在 firefox 21.0 中会抛出 TypeError?
Object.defineProperty(window,'windowProperty',{
get: function(){
return 'windowProperty'
},
set: function(val){
console.log('windowProperty is being set');
},
configurable: true,
});
var windowProperty;
Run Code Online (Sandbox Code Playgroud)
但是不使用 var 声明 windowProperty 是有效的:
windowProperty;
Run Code Online (Sandbox Code Playgroud)
或者
window.windowProperty;
Run Code Online (Sandbox Code Playgroud)
这种行为也存在于蜘蛛猴中:
var a=1;
Object.defineProperty(this,'a',{
get: function(){
return 'a';
},
});
Run Code Online (Sandbox Code Playgroud) 在jquery中是否有一个包装器方法或一些用于dispatchEvent的库?我一直在stackoverflow上寻找这个,但我发现的最接近的方法是jquery的trigger(),它似乎只触发jquery事件监听器.当然我可以自己使用dispatchEvent,但我想使用jquery使我的代码更容易阅读.
我正在编写一个greasemonkey脚本,我想向一些匿名事件监听器发送一个事件.页面本身不是用jquery编写的.
这里有一个jsfiddle链接来解释我想要实现的目标:http://jsfiddle.net/Zx3CA/
JS:
function log(s){
document.getElementById('log').innerHTML+=s+'<br/>';
}
$(function(){
//code on the page, which shouldn't be changed
//start
document.getElementById('link').addEventListener('click',function(){
log('click detected');
},false);
//end
$('#triggerClickJQuery').on('click',function(){
$('#link').trigger('click');
});
$('#triggerClickJS').on('click',function(){
var event=document.createEvent('MouseEvents');
event.initMouseEvent('click',true,true,null,-1,-1,-1,-1,0,true,false,false,true,0,null);
$('#link').get(0).dispatchEvent(event);
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<body>
<a id="link" href="javascript:void(0);">Link</a><br/>
<a id="triggerClickJQuery" href="javascript:void(0);">Trigger Click JQuery</a><br/>
<a id="triggerClickJS" href="javascript:void(0);">Trigger Click JavaScript</a><br/>
<div id="log"></div>
</body>
Run Code Online (Sandbox Code Playgroud)
提前致谢.
有更简单的方法来做以下事情吗?
(set() for _ in itertools.repeat(None))
Run Code Online (Sandbox Code Playgroud)
注意它是不同的itertools.repeat(set()),因为后者只构造一次set对象.
如何启用GTK 3.14附带的新GtkInspector?
更改日志表示Control+ Shift+ I或Control+ Shift+ D打开检查器,如果org.gtk.Settings.Debug.enable-inspector-keybinding设置为true.我符合上述所有条件,但在任何GTK 3应用程序中,这两种快捷方式都不适合我.
使用export GTK_DEBUG=interactive确实有效,但我想知道为什么快捷方式不行.
如何在 git 中显示提交所在的标签?
对于分支机构,这可以通过 轻松完成git branch --contains <commit>。
但是,如果通过使用标签引用标签但删除分支来进行归档,则标签可能根本不属于任何分支。
如何更改firefox调试器的步骤,跳过步骤,逐步取消键绑定?
找不到关于这个的任何文档或关于:config的任何内容.
我正在尝试并行化文件过滤操作,其中每个过滤器都是一个大的正则表达式,因此整个操作需要时间来运行。文件本身大约有 100GB。单进程版本如下所示:
def func(line):
# simple function as an example
for i in range(10**7):
pass
return len(line) % 2 == 0
with open('input.txt') as in_sr, open('output.txt', 'w') as out_sr:
for line in input:
if func(line):
out_sr.write(line)
Run Code Online (Sandbox Code Playgroud)
我尝试使用multiprocessing's ,imap但这让ValueError: I/O operation on closed file.我认为迭代器被复制到每个进程,但并非所有进程都打开该句柄。
有没有办法使用multiprocessing(最好是使用池)来做到这一点?
以下代码意外地引发了异常:pywintypes.error: (6, 'GetFileInformationByHandle', 'The handle is invalid.'),即GetFileInformationByHandle无效.
奇怪的是,在Python调试器下一切正常.更奇怪的是,当我删除some_parameter或GetFileInformationByHandle,错误消失.这告诉我,也许是一些内存错误,但我真的不知所措.
有些代码可能看起来没必要,但我不能在不引起异常的情况下缩小代码.
我已经在Windows 7,pywin32 218.5和219上的Python 3.4.1 x64上测试了这个.
import os
import win32file
import pywintypes
from ctypes import *
from ctypes.wintypes import *
class BY_HANDLE_FILE_INFORMATION(Structure):
_fields_ = [
('dwFileAttributes', DWORD),
('ftCreationTime', FILETIME),
('ftLastAccessTime', FILETIME),
('ftLastWriteTime', FILETIME),
('dwVolumeSerialNumber', DWORD),
('nFileSizeHigh', DWORD),
('nFileSizeLow', DWORD),
('nNumberOfLinks', DWORD),
('nFileIndexHigh', DWORD),
('nFileIndexLow', DWORD),
]
def GetFileInformationByHandle2(handle):
GetFileInformationByHandle(handle)
def GetFileInformationByHandle(handle):
bhfi = BY_HANDLE_FILE_INFORMATION()
res = windll.kernelbase.GetFileInformationByHandle(handle.handle, byref(bhfi))
if res == 0: …Run Code Online (Sandbox Code Playgroud) 可以通过以下方式模拟打印:
import unittest
import builtin
class TestSomething(unittest.TestCase):
@mock.patch('builtins.print')
def test_method(self, print_):
some_other_module.print_something()
Run Code Online (Sandbox Code Playgroud)
但是这意味着在python调试控制台(pydev调试器)和单元测试方法本身print不能使用.这很不方便.
有没有办法只在模拟print方法some_other_module而不是在测试模块中?
避免这种情况的一种方法是将测试模块中的print使用与其他一些刚调用的函数交换print,如果事实证明没有更好的解决方案,我可以这样做.
javascript ×3
python ×3
ctypes ×1
events ×1
firefox ×1
git ×1
gtk3 ×1
html ×1
iterator ×1
jquery ×1
mocking ×1
python-3.x ×1
unit-testing ×1
windows ×1