我正在尝试为js Array原型实现一个重复的方法,它将数组的副本连接到自身,如下所示:
[11,22,3,34,5,26,7,8,9].duplicate(); // [11,22,3,34,5,26,7,8,9,11,22,3,34,5,26,7,8,9]
Run Code Online (Sandbox Code Playgroud)
这是我的,但它导致浏览器崩溃:
var array = [11,22,3,34,5,26,7,8,9];
Array.prototype.duplicate = function() {
var j = this.length;
for(var i = 0; i < this.length; i++) {
this[j] = this[i];
j++;
}
return this;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用原生JS作为迭代和算法的实践,所以我试图避免内置方法,如果可能的话,这样我就可以更清楚地了解事情是如何被移动的.
关于它崩溃的原因以及我如何优化它的任何想法?
我在创建新的会话以执行命令时遇到了一些麻烦.
这是我的.tmux.conf的一部分:
set-window-option -g automatic-rename off
set-option -g allow-rename off
new -A -s 'main' -n 'servers' 'ls' # troubled line
splitw -h -p 35 htop
splitw -v
splitw -v -t 1
splitw -v -t 1
neww -n 'irc' weechat-curses
selectw -t 0
Run Code Online (Sandbox Code Playgroud)
这是我正在努力的路线:
new -A -s 'main' -n 'servers' 'ls'
Run Code Online (Sandbox Code Playgroud)
这是我打开tmux的方式:
alias tux='TERM=screen-256color-bce tmux -f ~/.tmux.conf attach-session -t main'
Run Code Online (Sandbox Code Playgroud)
'ls'必须导致错误,因为当它存在时,初始窗格不会被创建.如果我将其更改为"顶部",它可以正常工作并执行命令.
那么为什么顶级工作而不是ls(或我尝试的任何其他命令)?
我无法弄清楚如何仅存根对方法的两次调用中的一个。下面是一个例子:
class Example
def self.foo
{ a: YAML.load_file('a.txt'), # don't stub - let it load
b: YAML.load_file('b.txt') } # stub this one
end
end
RSpec.describe Example do
describe '.foo' do
before do
allow(YAML).to receive(:load_file).with('b.txt').and_return('b_data')
end
it 'returns correct hash' do
expect(described_class.foo).to eq(a: 'a_data', b: 'b_data')
end
end
end
Run Code Online (Sandbox Code Playgroud)
测试失败,因为我已经YAML.load_file用参数为第二个调用 ( 'b.txt') 而不是它遇到的第一个调用 ( )存根了一个调用'a.txt'。我认为参数匹配可以解决这个问题,但事实并非如此。
Failures:
1) Example.foo returns correct hash
Failure/Error:
{ a: YAML.load_file('a.txt'),
b: YAML.load_file('b.txt') }
Psych received :load_file with unexpected arguments
expected: ("b.txt") …Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,但为什么下面的循环在Chrome中进入无限循环但在Firefox中却没有?(显然,循环测试是它失败的地方 - 我只是不知道为什么).
for(var i = 0; localStorage[this.config.localStoragePrefix + i] != 'undefined'; i++)
this.config.appCount++;
Run Code Online (Sandbox Code Playgroud)
它正在检查存在多少localStorage元素.例如:
localStorage['myPrefix_0']
localStorage['myPrefix_1']
localStorage['myPrefix_2'] ...
Run Code Online (Sandbox Code Playgroud)
会回来3.
有关为什么在Chrome中永远循环的任何想法?
我有以下内容:
class Foo:
def __init__(self, **kwargs):
print kwargs
settings = {foo:"bar"}
f = Foo(settings)
Run Code Online (Sandbox Code Playgroud)
这会产生错误:
Traceback (most recent call last):
File "example.py", line 12, in <module>
settings = {foo:"bar"}
NameError: name 'foo' is not defined
Run Code Online (Sandbox Code Playgroud)
如何正确地将键/值args的字典传递给kwargs?
我有config/index.js基于设置的NODE_ENV环境变量返回不同的配置文件.
我正在尝试编写一个简单的测试来确保为每个环境返回正确的配置,但我遇到的问题是,实际上只调用了第一个需要,并且后续需要同一个文件正在使用来自第一个要求.
我该如何更改测试以解决此问题?
describe('config', function () {
it('should return dev config', function (done) {
process.env.NODE_ENV = 'development';
var config = require(__dirname + '/../../config'); // development config
console.log(config.plugins.ipFilter);
done();
});
it('should return prod config', function (done) {
process.env.NODE_ENV = 'production';
// development config from above.
// the require here doesn't actually get invoked
var config = require(__dirname + '/../../config');
console.log(config.plugins.ipFilter);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
这是一个简化版本config/index.js(工作正常),我正在尝试测试:
var Hoek = require('hoek');
var settings = {
'defaults': require('./settings/defaults'),
'production': require('./settings/production')
}; …Run Code Online (Sandbox Code Playgroud) 出于学习目的,我正在研究是否可以转换以下三个方法调用:
Foo::Bar.logger.debug(a)
Foo::Bar.logger.debug(b)
Foo::Bar.logger.debug(c)
Run Code Online (Sandbox Code Playgroud)
使用速记proc &method方法转换为单行语句:
[a, b, c].each(&method(:'Foo::Bar.logger.debug'))
Run Code Online (Sandbox Code Playgroud)
.debug不.to_proc自然地回应:
NameError: undefined method `Foo::Bar.logger.debug' for class `#<Class:Foo>'
Run Code Online (Sandbox Code Playgroud)
这确实有效;但是,它不像前者那么简洁:
logger = Proc.new { |x| Foo::Bar.logger.debug(x) }
[a, b, c].each(&logger)
Run Code Online (Sandbox Code Playgroud)
前一种方法可行吗?