使用MomentJS,如果需要,我可以使用.locale(或.lang在旧版本中)在特定时刻实例(而不是全局)上设置区域设置.如何使用除全局区域之外的特定区域设置解析字符串?由于moment函数本身就是我们用于解析的东西,并且似乎.set没有完整解析的变体?
例如:
// Assume the default locale is 'en' (just in case, I'll set it for the snippet)
moment.locale('en');
// I have this string that's in the French locale
var str = "30 Avril 2015";
// Parsing it fails
var m = moment(str, "DD MMMM YYYY");
snippet.log(m.format("YYYY-MM-DD")); // "Invalid Date"Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>Run Code Online (Sandbox Code Playgroud)
我从api xml数据获取,其中一个xml元素是日期时间,nodeValue始终采用这种格式 - 字符串:"YYYY-MM-DD".(我无法从api请求,以不同格式返回日期时间)
我的问题是将此格式拆分并转换为此字符串:"DD.MM.YYYY"
基本上我这样做了:
var myString = "2015-04-10"; //xml nodeValue from time element
var array = new Array();
//split string and store it into array
array = myString.split('-');
//from array concatenate into new date string format: "DD.MM.YYYY"
var newDate = (array[2] + "." + array[1] + "." + array[0]);
console.log(newDate);
Run Code Online (Sandbox Code Playgroud)
这是jsfiddle:http://jsfiddle.net/wyxvbywf/
现在,这段代码可行,但我的问题是:有没有办法以更少的步骤获得相同的结果?
var metricUnits = function(num) {
console.log(num.toString().length);
};
// This works, gives me 21
metricUnits(900000000000000000000);
// But this fails, gives me 5
metricUnits(9000000000000000000000);
Run Code Online (Sandbox Code Playgroud)
当我调用此函数时,21将记录到控制台.但是,当我在输入参数的末尾添加一个或多个零时,将5打印到控制台?!为什么是这样?
我读到只做& 0x7fffffff符号位的掩码,并没有篡改其他位.
int a = Integer.MIN_VALUE;
System.out.println(a & 0x7fffffff);
Run Code Online (Sandbox Code Playgroud)
但是,这段代码输出
0
Run Code Online (Sandbox Code Playgroud)
代替
2147483648
Run Code Online (Sandbox Code Playgroud)
这是为什么?
一个PHP脚本正在计算我以下数组:
$test = Array();
$test['a'] = Array();
$test['a']['a1'] = 'a1';
$test['a']['a2'] = 'a2';
$test['b'] = Array();
$test['b']['b1'] = 'b1';
$test['b']['b2'] = 'b2';
Run Code Online (Sandbox Code Playgroud)
我正在使用以下方法将此数组转换为JSON:
echo json_encode($test);
Run Code Online (Sandbox Code Playgroud)
我正在使用Ajax调用检索此JSON,我正在使用以下方法将其转换为JavaScript数组:
test = JSON.parse(data);
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何在JavaScript中向此数组添加条目?我试过了:
test['c'] = [];
test['c']['c1'] = 'c1';
test['c']['c2'] = 'c2';
Run Code Online (Sandbox Code Playgroud)
但是然后在控制台中test['c']是空的(Array[0]).
下面的脚本将替换实际图像对象的所有图像标记.不幸的是,我收到了一个Uncaught TypeError错误.这是因为在我的示例中,Javascript从0计数到3,而不是1到4.数组的长度为4.
如何避免此错误(因此我也在while循环后获得控制台日志)?
var raw_content = {
"text" : "Test image 1 [image] Test image 2 [image] Test image 3 [image] Test image 4 [image]",
"media" :[
{
"src": "http://placehold.it/400x200",
"caption": "This is a caption"
},{
"src": "images/gallery/sh0.png",
"caption": "This is a caption"
},{
"src": "http://placehold.it/400x200",
"caption": "This is a caption"
},{
"src": "images/gallery/sh0.png",
"caption": "This is a caption"
}
]
};
// find img one by one
var image_counter = 0;
var text_buffer = raw_content.text; …Run Code Online (Sandbox Code Playgroud) 我在nodejs上运行此代码.我想知道为什么闭包执行时不打印字符串'Globals'?是不是this在关闭指向全球范围?
// Running on NodeJS, not in a browser!
this.name = "Globals";
function Person(name) {
this.name = name;
this.namePrinter = function() {
return function() {
console.log(this.name);
}
}
}
var p = new Person("Faiz");
p.namePrinter()(); // prints undefined. Shouldn't it print Globals?
console.log(this.name); // prints Globals
Run Code Online (Sandbox Code Playgroud) 我通过Google Chrome版本57.0.2987.133运行以下脚本:
var loggingProxyHandler = {
"get" : function(targetObj, propName, receiverProxy) {
let ret = Reflect.get(targetObj, propName, receiverProxy);
console.log("get("+propName.toString()+"="+ret+")");
return ret;
},
"set" : function(targetObj, propName, propValue, receiverProxy) {
console.log("set("+propName.toString()+"="+propValue+")");
return Reflect.set(targetObj, propName, propValue, receiverProxy);
}
};
function onRunTest()
{
let m1 = new Map();
let p1 = new Proxy(m1, loggingProxyHandler);
p1.set("a", "aval"); // Exception thrown from here
}
onRunTest();Run Code Online (Sandbox Code Playgroud)
NOTE: Requires a browser supporting ES2015's ProxyRun Code Online (Sandbox Code Playgroud)
运行时,我看到调用处理程序的get陷阱来返回Map的set函数,然后我收到以下错误:
"Uncaught TypeError: Method Map.prototype.set called on incompatible receiver [object Object]"
at Proxy.set …Run Code Online (Sandbox Code Playgroud) 有人可以向我解释为什么下面的代码返回audiences而不是返回一个空数组吗?
return Audience.find()
.exec((err, audiences) => {
if (err) return errorHandler.handle('audienceService', err);
return Promise.resolve([]);
});
Run Code Online (Sandbox Code Playgroud) 我已经定义了类型,但我仍然收到一条错误消息:
绑定元素“title”隐式具有“any”类型。
在这一行的 id、title 和 body
static getInitialProps({ query: { id, title, body } }) {
Run Code Online (Sandbox Code Playgroud)
这是我的代码: import React, { Component } from 'react';
type Props={
id: number;
title: string;
body: string;
postId: string;
}
export default class extends Component <Props> {
static getInitialProps({ query: { id, title, body } }) {
return { postId: id, title, body }
}
render() {
return (
<div>
<h1>My blog post #{this.props.postId}</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do …Run Code Online (Sandbox Code Playgroud) javascript ×8
arrays ×1
bit ×1
closures ×1
dictionary ×1
es6-promise ×1
java ×1
momentjs ×1
mongodb ×1
mongoose ×1
next.js ×1
node.js ×1
operations ×1
php ×1
proxy ×1
reactjs ×1
scope ×1
split ×1
string ×1
types ×1
typescript ×1