如何将参数应用于String.prototype.format函数
How to apply arguments to String.prototype.format function
我试图在谷歌上搜索如何将参数动态应用于String.prototype.format函数,但我找不到任何我能理解的东西。colsEdit数组可以包含可变数量。
if (!String.prototype.format) {
String.prototype.format = function () {
var args = arguments;
return this.replace(/{('d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match;
});
};
}
colsEdit =
[
{ name: 'Amount', text: 'Amount', required: true, type: 'Numeric', value: 0, default: '' },
{ name: 'Date', text: 'Date', required: true, type: 'Date', value: '', default: new Date().format('yyyy-MM-dd') },
{ name: 'CostTypeId', text: 'Cost type', required: true, type: 'Select', value: 0, default: 1, dataSource: [] },
{ name: 'CurrencyId', text: 'Currency', required: true, type: 'Select', value: 0, default: 9, dataSource: [] },
{ name: 'CountryId', text: 'Country', required: true, type: 'Select', value: 0, default: 16, dataSource: [] },
{ name: 'Description', text: 'Description', type: 'Text', value: '', default: '' }
];
var sql = 'insert into MON_Costs (Amount, Date, CostTypeId, CurrencyId, CountryId, Description) values({0}, STRFTIME("%Y-%m-%d", "{1}"), {2}, {3}, {4}, "{5}")';
var args = [];
var id = 1;//just test
for (var i = 0; i < colsEdit.length; i++) {
args.push($('#__{0}'.format(colsEdit[i].name)).val());
}
args.push(id);
sql = sql.format(); //how to add args to format function???
您正在寻找函数的apply
方法:
sql = sql.format.apply(sql, args);
相关文章:
- 直接在函数声明上使用function.prototype.bind
- 如何迭代Array.prototype函数
- Object.prototype using 'this'
- 在不兼容的接收器上调用的方法Set.prototype.add未定义
- 为什么可以't我用Set对象调用Array.prototype.map
- ExtJS 4 Object.prototype fail
- 如何通过Prototype或jquery移除子类的父类基类
- 使用.format在一些HTML代码中插入文本,会出现错误(Python)
- 复选框:使用Array.prototype.forEach调用推送选中订单,
- Format for Excel VBA addWatermarkFromText
- 将setTimeout()包装器实现为Element.prototype中的方法
- Object.prototype.hasOwnProperty.call() vs Object.prototype.h
- 如何通过 Function.prototype.apply() 更改调用函数的参数
- "responsed_to|format|“;引发UnknownFormat错误
- Function.prototype.call和Function.protoype.all只应用一个参数
- 使用JavaScript或Prototype获取表单中的所有控件并启用它们
- JavaScript-使用B.prototype=new A()继承数组
- 转换类似数组的对象Array.prototype.slice或Array.from
- 如何将参数应用于String.prototype.format函数
- Node.js util.format() as String.prototype