我正在寻找一个很好的JavaScript等价的C/PHP printf()或C#/ Java程序员String.Format()(IFormatProvider对于.NET).
我的基本要求是现在有一千个数字分隔符格式,但处理许多组合(包括日期)的东西会很好.
我意识到Microsoft的Ajax库提供了一个版本String.Format(),但我们不希望该框架的整个开销.
我知道它用于使参数成为一个真正的数组,但我不明白使用时会发生什么 Array.prototype.slice.call(arguments)
我可以在JavaScript中以方便的方式调用带有参数数组的函数吗?
例:
var fn = function() {
console.log(arguments);
}
var args = [1,2,3];
fn(args);
Run Code Online (Sandbox Code Playgroud)
我需要的arguments是[1,2,3],就像我的数组.
我正在使用非常有用的本地胖箭头来保留this回调中的上下文.但是,有时我需要访问this如果我没有使用胖箭头就会有的值.
一个例子是事件回调,其中this包含事件发生的元素的值(我知道在这个特定的例子中你可以使用event.currentTarget,但我们假设你不能为了一个例子):
function callback() {
// How to access the button that was clicked?
}
$('.button').click(() => { callback() });
Run Code Online (Sandbox Code Playgroud)
注意:我遇到过这个问题,它解决了同样的问题,但是在CoffeeScript中.
我有一个函数,message它需要一个参数来定义消息的类型,然后它连接任何其他参数来形成消息,纯粹是一个精确的.
它看起来像这样:
function message(type) {
var msg = _.rest(arguments).join(" ");
// Really the type will be used to set the class on a div
// But I'm just using console.log to keep it simple for now.
console.log(type + ": " + msg);
}
Run Code Online (Sandbox Code Playgroud)
我想提供辅助功能,error,warning,info,它只需调用message与正确的类型.我只是不确定最好的方法来解决这个问题.我无法想到两种方法,但我不确定我是否正确地进行了这项工作,或者说我可能过于复杂.
第一种方式似乎有点多余,创建一个包含第一个arg和参数的新数组然后将其展平.
message.apply(this, _.flatten(["error", arguments]));
Run Code Online (Sandbox Code Playgroud)
第二种方式感觉有点......凌乱?
Array.prototype.unshift.call(arguments, "error");
message.apply(this, arguments);
Run Code Online (Sandbox Code Playgroud)
虽然从我的经验来看:
(function() {
Array.prototype.unshift.call(arguments, 0);
console,log(arguments);
})(1, 2, 3);
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
[0, 1, 2, 3, undefined, undefined, undefined, …Run Code Online (Sandbox Code Playgroud) 我有两个函数a()和b(),两者都是可变函数,让我说当我调用函数a()时这样:
a(arg0, arg1, arg2, arg3, ...., argn);
Run Code Online (Sandbox Code Playgroud)
然后函数b()也将在a()中调用,但是在()的参数列表中没有第一个参数"arg0":
b(arg1, arg2, arg3, ...., argn);
Run Code Online (Sandbox Code Playgroud)
有什么办法吗?
所以这段代码完美无缺
var arr = [1, 2, 3, 4];
arr.forEach(function (el) {
console.log(el);
})
Run Code Online (Sandbox Code Playgroud)
但如果我尝试这样做:
function printArgsInfo() {
arguments.forEach(function (el) {
console.log(el);
});
}
printArgsInfo(2, 3, 2.5, -110.5564, false);
Run Code Online (Sandbox Code Playgroud)
参数.forEach不是函数即使tought参数是一个数组,如果尝试用forin循环执行它仍然有效.
我知道有两种方法可以将类似Array的对象转换为Array.
Array.prototype.slice.call(arguments)Array.from(arguments)我想知道它们之间的区别是什么,我应该使用哪一个来转换类似数组的对象.
关于承诺的另一个问题.我有这种情况:
Service.prototype.parse = function (data) {
var deferred = $.Deferred();
var arr = [];
for (var i = 0; i < data.length; i++) {
var details = new Details();
$.when(details).then(function (data) {
arr.push(data);
deferred.resolve(arr);
});
}
return deferred.promise;
};
Run Code Online (Sandbox Code Playgroud)
代码中的其他地方:
...
$.when(parse()).then(function (resp) {
//...
});
Run Code Online (Sandbox Code Playgroud)
承诺在某些时候得到解决,但最初resp的长度为1.
如何等待parse()解决所有问题并返回数组?
如何使用.forEach代替for循环?
'use strict';
var score = (function(){
function updateScore() {
for(var i = 0; i < arguments.length; i++) {
this.score += arguments[i];
}// I want to use .forEach here instead of for loop.
return this.score;
}
return {
update: updateScore
}
})();
var soccer = {
name: 'Soccer',
score: 0
}
score.update.apply(soccer, [1,2,3])
console.log(soccer.score)
Run Code Online (Sandbox Code Playgroud)
这将记录6。
我试过了
function updateScore() {
arguments.forEach((args, i) => {
this.score += args[i];
};
return this.score;
};
Run Code Online (Sandbox Code Playgroud)
错误日志:arguments.forEach不是函数