我想找到许多只做1个查询的随机记录.
我试过了:
var count = db.collections.count()
var rand = function(){return Math.floor( Math.random() * count )}
db.collection.find().limit(-1).skip(rand()).next();
Run Code Online (Sandbox Code Playgroud)
但是这只返回一个文档.我需要获得更多随机记录.
我怎样才能做到这一点?
我想toString为数据类型创建自己的函数.
我们来举个例子:
JSON.stringify({}) // "{}"
Run Code Online (Sandbox Code Playgroud)
我想要"test"归还.所以,我试图修改对象原型:
Object.prototype.toString = function () { return "test"; }
Run Code Online (Sandbox Code Playgroud)
然后:也JSON.stringify({})回来"{}"了.
我确信有一个函数可以重写以返回自定义值.
那是什么功能?
node.js help命令输出以下内容:
debug> help
Commands: run (r), cont (c), next (n), step (s), out (o), backtrace (bt), setBreakpoint (sb), clearBreakpoint (cb),
watch, unwatch, watchers, repl, restart, kill, list, scripts, breakOnException, breakpoints, version
Run Code Online (Sandbox Code Playgroud)
我可以使用setBreakpoint以下命令添加新的断点:
debug> setBreakpoint(12)
...
Run Code Online (Sandbox Code Playgroud)
但我可以让它有条件吗?例如:
*only if `foo() === true`, stop here*
Run Code Online (Sandbox Code Playgroud)
另一种方法是if在脚本中添加它:
if (foo()) { debugger; }
Run Code Online (Sandbox Code Playgroud)
这是通过NodeJS调试器实现的吗?
$(window).on("keydown", function (e) {
alert(e.keyCode);
});
Run Code Online (Sandbox Code Playgroud)
W有87关键代码。我也知道,e.ctrlKey就是true如果Ctrl在那一刻被按下。
是否可以抓住Ctrl+ W键并阻止制表符关闭?
我试过了:
$(window).on("keydown", function (e) {
if (e.keyCode === 87 && e.ctrlKey) {
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,似乎浏览器快捷方式具有优先权,并且该选项卡已关闭。
我正在寻找一种解决方案:如果无法通过JavaScript进行操作,则可能是浏览器插件可以执行此操作。
在VIM宏中,如何处理条件操作?
if condition is true
do this
else
do something else
Run Code Online (Sandbox Code Playgroud)
文件内容:
_
abcdefg
Run Code Online (Sandbox Code Playgroud)
宏将做:
G^
if letter is one of the following letters: a e i o u
gg$i0<ESC>
else
gg$i1<ESC>
Gx
Run Code Online (Sandbox Code Playgroud)
重复7次缓冲区将是:
_0111011
Run Code Online (Sandbox Code Playgroud)
那么,我如何验证条件是否为真然后运行一个动作?
我想构建一个正在运行的小脚本,它应该创建一个类似 bash 的会话(在当前的 bash 会话中,创建进程的地方),稍后可以用于一些疯狂的科学(例如管道到浏览器)。
我尝试使用pty.js,管道stdin到bash进程,以及从 bash 会话到stdout流的数据:
var pty = require("pty.js");
var term = pty.spawn('bash', [], {
name: 'xterm-color',
cols: process.stdout.columns,
rows: process.stdout.rows,
cwd: ".",
env: process.env
});
term.pipe(process.stdout);
process.stdin.pipe(term);
term.on("close", function () {
process.exit();
});
Run Code Online (Sandbox Code Playgroud)
这有效,但它非常有问题:

例如,不会捕获非字符(方向键、制表符等)。
我也尝试使用spawn,这还不错,但仍然有问题。
var spawn = require("child_process").spawn;
var bash = spawn("bash");
bash.stdout.pipe(process.stdout);
process.stdin.pipe(bash.stdin);
Run Code Online (Sandbox Code Playgroud)
有没有更好的解决方案如何在 NodeJS 中创建 bash 包装器?
Git接受许多不同的URL格式(例如ssh,http,https等)。有什么规格/官方文档可以找到受支持的git url格式吗?
我写了一个git url解析器,我想确保在那里做的是正确的。
在这里,在YonderGit上,我找到了以下列表。由于https://<token>:x-oauth-basic@host.xz/path/to/repo.git不存在,因此不完整。
安全外壳传输协议
ssh://user@host.xz:port/path/to/repo.git/ssh://user@host.xz/path/to/repo.git/ssh://host.xz:port/path/to/repo.git/ssh://host.xz/path/to/repo.git/ssh://user@host.xz/path/to/repo.git/ssh://host.xz/path/to/repo.git/ssh://user@host.xz/~user/path/to/repo.git/ssh://host.xz/~user/path/to/repo.git/ssh://user@host.xz/~/path/to/repo.gitssh://host.xz/~/path/to/repo.gituser@host.xz:/path/to/repo.git/host.xz:/path/to/repo.git/user@host.xz:~user/path/to/repo.git/host.xz:~user/path/to/repo.git/user@host.xz:path/to/repo.githost.xz:path/to/repo.gitrsync://host.xz/path/to/repo.git/Git传输协议
git://host.xz/path/to/repo.git/git://host.xz/~user/path/to/repo.git/HTTP / S传输协议
http://host.xz/path/to/repo.git/https://host.xz/path/to/repo.git/本地(文件系统)传输协议
/path/to/repo.git/path/to/repo.git/~/path/to/repo.gitfile:///path/to/repo.git/file://~/path/to/repo.git/
我正在使用日期输入类型从HTML表单显示/获取日期:
<input type="date">
Run Code Online (Sandbox Code Playgroud)
要通过JavaScript设置日期,我需要:
myInput.valueAsDate = new Date();
Run Code Online (Sandbox Code Playgroud)
这很好。但是,如果要设置另一个日期对象,例如:
myInput.valueAsDate = new Date(1995, 0, 1);
Run Code Online (Sandbox Code Playgroud)
它显示12/31/1994–这是1995年除夕前一天的准确记录。如果我添加24的时间参数,日期显示正确,但日期对象本身显然是2 次 1995年1月的,这不是一个很好的解决这个问题。
<input type="date">
Run Code Online (Sandbox Code Playgroud)
myInput.valueAsDate = new Date();
Run Code Online (Sandbox Code Playgroud)
我在控制台中得到这个:
Sun Jan 01 1995 00:00:00 GMT+0200 (EET)
Sat Dec 31 1994 02:00:00 GMT+0200 (EET)
Run Code Online (Sandbox Code Playgroud)
这是浏览器问题吗?有什么解决方法/解决方案?
最后,我不希望使用任何插件来显示/获取时间,但我想用natative日期input 元素(至少,这个问题是关于它:-))。
我在Chromium和Chrome中复制了此内容。火狐好像不支持日期类型输入还。
如何为类,id或其他CSS选择器覆盖整个CSS样式?
例如:
如果在styles1.css我有:
/* also, this file contains a lot of styles used on other pages */
.one-great-class {
background: white
...
/* a lot of properties */
}
Run Code Online (Sandbox Code Playgroud)
...(在styles2.css(仅在一个网页中使用)我想one-great-class 完全覆盖我写的内容?
.one-great-class {
/* Is possible that a line of code to delete all styles from this class? */
}
Run Code Online (Sandbox Code Playgroud) 这里有什么问题?当我点击时#background_audio div,音频淡出然后自动淡入并继续播放而不再按下按钮.我如何解决它?
我试图使音频淡出并在单击#background_audio时暂停,然后#background_audio再次单击时,音频淡入并恢复.
<script type="text/javascript">
$(document).ready(function(){
$('#mute').click(function(){
$('#background_audio').animate({volume: 0}, 1000);
setTimeout(function(){('#background_audio').pause()},1000);
});
$('#mute').click(function(){
$('#background_audio').animate({volume: 1}, 1000);
setTimeout(function(){('#background_audio').pause()},1000);
});
});
</script>
Run Code Online (Sandbox Code Playgroud)