为了在javascript中取时间戳,你可以编写这种代码:
// Usual Way
var d = new Date();
timestamp = d.getTime();
Run Code Online (Sandbox Code Playgroud)
但我发现也可以通过这种方式获得相同的结果:
// The shortest Way
timestamp = +new Date();
Run Code Online (Sandbox Code Playgroud)
有人可以帮我理解Shortest Way的工作原理吗?
在JavaScript中,将格式良好的string("4,5,7,2")转换为数组([4,5,7,2])的最佳方法是什么:
var _intStr="3,5,7,8" → var _intArr=[3,5,7,8]
这是我的代码:
function addRcd2(timeOut){
for(var c=0; c less 5; c++){
var rcdi = "rcd_"+c+"";
setTimeout(function(){
$('.tbl1 tbody').append(rcdi);
},timeOut*c);
}
}
此代码的输出是一个表,其中行具有相同的文本rcd_5.
我的目标是让表行具有不同的记录rcd_1,...... , rcd_5.
有任何想法吗?
我试图使用匿名函数添加处理程序锚定对象.
我运行此代码,但它不起作用你能解释原因并尝试修复它吗?谢谢:
var obj = document.getElementsByTagName("a");
var items = ["mouseover", "mouseout"];
for (var i = 0; i < items.length; i++) {
(function() {
var item = items[i];
for (var i = 0; i < obj.length; i++) {
obj[i]["on" + item] = function() {
alert("thanks for your " + item);
};
}
})();
}
Run Code Online (Sandbox Code Playgroud) 我想了解以下行为,因为对这个网站javascript garden的解释对 我来说还不够.
如果您能够对内联评论中的问题给出明确的解释,将不胜感激.
这里的例子如下:
function Foo() {}
Foo.prototype.method = function(a, b, c) {
console.log(this, a, b, c);
};
Foo.method = function() {
Function.call.apply(Foo.prototype.method, arguments);
};
Foo.prototype.method(1,2,3) // Foo { method=function()} 1 2 3 //this output is obvious
Foo.method(1,2,3) // Number {} 2 3 undefined // I want understand why the first argument is a number and the last one is undefined
Run Code Online (Sandbox Code Playgroud) 我试图实现以下代码:
var action = function (e) {
if (!e) {
var e = window.event;
}
e.cancelBubble = true;
if (e.stopPropagation) {
e.stopPropagation();
}
container.objet.hide();
}
Run Code Online (Sandbox Code Playgroud)
但jslint抱怨以下内容:
'e' is already defined. var e = window.event;
解决此问题的最佳方法是什么?
如果您尝试生成一个非常长的数组的console.log,firebug控制台只显示完整结果的一部分.
例:
arr = [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],["casa","auto"],["casa","auto","casa","auto","casa","auto","casa","auto","casa","auto","casa","auto","casa","auto","casa","auto",[1,2,3],[1,2,3],[1,2,3],[1,2,3]]]
console.log(arr); // .... ["casa", "auto"], ["casa", "auto", "casa", 17 more...]]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它显示数组的第一部分,然后显示单词 17 more...]]
如何显示所有数组结果?
我想复制和过去.不幸的是,console.dir返回一个javascript对象浏览器.
我想知道是否有一些java脚本库用于获取具有不同语言的Facebook格式的日期/时间?
facebook格式的例子是:
about an hour ago
7 minutes ago
Yesterday at 03:48
Sunday at 18:00
13 mars, à 21:34
11 septembre 2001, à 8:45
Run Code Online (Sandbox Code Playgroud) 似乎在JavaScript中你不能使用delete函数参数,但你可以delete从函数中获取全局变量.
为什么会这样?
var y = 1;
(function (x) { return delete y; })(1); // true
(function (x) { return delete x; })(1); // false
Run Code Online (Sandbox Code Playgroud) 以下编写javascript函数的方法是等效的.
也许第一个更清楚.
然而,许多程序员更喜欢第二种方式.
优先选择第二种方式的两种方式有显着差异吗?
第一种方式:
Class.prototype.fn = function () {
var obj = {
…
};
return obj;
};
Run Code Online (Sandbox Code Playgroud)
第二种方式:
Class.prototype.fn = function () {
return {
.........
};
};
Run Code Online (Sandbox Code Playgroud) 可能重复:
从字符串的开头和结尾修剪空格
我想找到一种方法来实现以下结果:
"I am\n a string \n\n \n \n".replace(/\n{1,}$/g,""); // "I am\n a string"
Run Code Online (Sandbox Code Playgroud)
什么是最好的方法?
在 c++ 中,如果使用这些类型的大括号:
if (x == y)
{ <---
} <---
Run Code Online (Sandbox Code Playgroud)
是否可以创建一个使用这些的类?与此类似的地方:
class x
{
operator {} foo(int something)
{
do something...
}
};
Run Code Online (Sandbox Code Playgroud)
附注。我不知道这些括号叫什么,大括号?
谢谢你
编写优秀代码的基本思想是代码必须是可读的,良好的注释和记录.
编写关于所有程序语言的优秀代码的其他基本事实是构建目录和文件中的代码以及缩进当然.
在javascript的情况下,事情可能会更复杂,因为语言允许您使用不同的样式来完成类似的任务.
例如,有三种不同的方法来定义JavaScript类.
说,哪些是最好的资源(书籍/网站)或建议在javascript中编写好的代码(库)?