jQuery模板已经被弃用了一段时间了.
我有一些JavaScript对象形式的数据,我想格式化为HTML并附加到DOM.这些天最好的办法是什么?
$('<li>',{id:'my-'+Id}).append($('<span>').text(myText))吗?这是我需要做的.
获取日期,转换为字符串并将其传递给第三方实用程序.当我通过时,库中的响应将以字符串格式显示日期.所以,我需要将日期转换为字符串,如20110506105524(YYYYMMDDHHMMSS)
function printDate() {
var temp = new Date();
var dateStr = temp.getFullYear().toString() +
temp.getMonth().toString() +
temp.getDate().toString() +
temp.getHours().toString() +
temp.getMinutes().toString() +
temp.getSeconds().toString();
debug (dateStr );
}
Run Code Online (Sandbox Code Playgroud)
上面的问题是,对于1-9个月,它会打印一个数字.如何更改它以正确打印月份,日期的2位数...
Python有这个美丽的功能来解决这个问题:
bar1 = 'foobar'
bar2 = 'jumped'
bar3 = 'dog'
foo = 'The lazy ' + bar3 + ' ' + bar2 ' over the ' + bar1
# The lazy dog jumped over the foobar
Run Code Online (Sandbox Code Playgroud)
进入:
bar1 = 'foobar'
bar2 = 'jumped'
bar3 = 'dog'
foo = 'The lazy {} {} over the {}'.format(bar3, bar2, bar1)
# The lazy dog jumped over the foobar
Run Code Online (Sandbox Code Playgroud)
JavaScript有这样的功能吗?如果没有,我将如何创建一个遵循与Python实现相同的语法?
String.Format不起作用TypeScript.
错误:
Run Code Online (Sandbox Code Playgroud)The property 'format' does not exist on value of type '{ prototype: String; fromCharCode(...codes: number[]): string; (value?: any): string; new(value?: any): String; }'.
attributes["Title"] = String.format(
Settings.labelKeyValuePhraseCollection["[WAIT DAYS]"],
originalAttributes.Days
);
Run Code Online (Sandbox Code Playgroud) 如何在GWT中格式化我的字符串?
我做了一个方法
Formatter format = new Formatter();
int matches = 0;
Formatter formattedString = format.format("%d numbers(s, args) in correct position", matches);
return formattedString.toString();
Run Code Online (Sandbox Code Playgroud)
但它抱怨道
Validating newly compiled units
[ERROR] Errors in 'file:/C:/Documents%20and%20Settings/kkshetri/workspace/MasterMind/MasterMind/src/com/kunjan/MasterMind/client/MasterMind.java'
[ERROR] Line 84: No source code is available for type java.util.Formatter; did you forget to inherit a required module?
Run Code Online (Sandbox Code Playgroud)
不包括Formatter吗?
如果小时小于10小时,则通常以小时数的形式放置小时.
var currentHours = currentTime.getHours ( );
Run Code Online (Sandbox Code Playgroud)
以下是将时间显示为最佳方式09而不是9?
if (currentHours < 10) currentHours = '0'+currentHours;
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以格式化Javascript模板字符串中的数字,例如:
var n = 5.1234;
console.log(`This is a number: $.2d{n}`);
// -> 5.12
Run Code Online (Sandbox Code Playgroud)
或者可能
var n = 5.1234;
console.log(`This is a number: ${n.toString('.2d')}`);
// -> 5.12
Run Code Online (Sandbox Code Playgroud)
这种语法显然不起作用,它只是我正在寻找的事物类型的一个例子.
我知道像sprintffrom underscore.string这样的工具,但这似乎是JS应该能够开箱即用的东西,特别是考虑到模板字符串的强大功能.
编辑
如上所述,我已经知道第三方工具(例如sprintf)和自定义功能.类似的问题(例如,相当于printf/String.Format的JavaScript)根本没有提到模板字符串,可能是因为在ES6模板字符串出现之前就被问过了.我的问题是针对ES6的,并且与实施无关.我很高兴接受一个答案"不,这是不可能的",如果是这样的话,但是最好的是有关提供此功能的新ES6功能的信息,或者是否有关于此功能是否在其上的一些信息办法.
这似乎是一个常见的JavaScript用法:
function foo (array, index) {
if (typeof array[index] == 'undefined')
alert ('out of bounds baby');
}
Run Code Online (Sandbox Code Playgroud)
而不是更普遍(在其他语言中)和概念上更简单:
function foo (array, index) {
if (index >= array.length)
alert ('boo');
}
Run Code Online (Sandbox Code Playgroud)
据我所知,第一种情况也适用于其中有"间隙"的阵列,但这是一个足以保证成语的常见情况吗?
可以在此处看到提示此问题的代码示例.在这种情况下,当在函数内部使用'argument'变量时,假设它是一个连续的数组是不是很明智?
Chrome中有没有办法(如扩展程序或应用程序)在Chrome中创建和运行.js文件?
我已经整理了一个非常简单的程序,它使用JavaScriptCore来评估JS:
#import <CoreFoundation/CoreFoundation.h>
#import <JavaScriptCore/JavaScriptCore.h>
int main(int argc, const char * argv[])
{
JSGlobalContextRef ctx = JSGlobalContextCreate(NULL);
FILE *f = fopen(argv[1],"r");
char * buffer = malloc(10000000);
fread(buffer,1,10000000,f);
CFStringRef strs = CFStringCreateWithCString(NULL, buffer, kCFStringEncodingASCII);
JSStringRef jsstr = JSStringCreateWithCFString(strs);
JSValueRef result = JSEvaluateScript(ctx, jsstr, NULL, NULL, 0, NULL);
double res = JSValueToNumber(ctx, result, NULL);
JSGlobalContextRelease(ctx);
printf("%lf\n", res);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里的想法是最后一个值应该是a Number,并打印该值.这适用于有效的JavaScript代码,例如
var square = function(x) { return x*x; }; square(4)
Run Code Online (Sandbox Code Playgroud)
但是,如果代码尝试执行a console.log,则程序会出现段错误.JSC中是否有可用的日志功能,还是我必须自己滚动?
javascript ×8
string ×2
arrays ×1
date ×1
ecmascript-6 ×1
format ×1
gwt ×1
html ×1
java ×1
jquery ×1
json ×1
macos ×1
objective-c ×1
python ×1
templates ×1
typescript ×1
xcode ×1