有多种方法可以使用Dateobject 获取当前时间(以毫秒为单位):
(new Date()).getTime();
+new Date();
Date.now();
Run Code Online (Sandbox Code Playgroud)
假设你不需要创建一个对象而只需要一个以毫秒为单位的当前时间,哪一个是最有效的?在性能方面.
编辑:我理解大多数开发人员都不关心这一点,但是当你在低技术的嵌入式环境中工作或者只是为了消除好奇心时,这可能很重要.
我经常发现自己正在使用这样的深层对象:
var x = {
y: {
z: {
a:true
}
}
}
Run Code Online (Sandbox Code Playgroud)
在代码的某处:
if( x.y.z.a === true ){
//do something
}
Run Code Online (Sandbox Code Playgroud)
并且在某些情况下,任何x,y,z变量都可能未定义,在这种情况下,您将获得" 无法读取未定义的属性* "
可能的解决方案是
if( x && x.y && x.y.z && x.y.z.a === true ){
//do something
}
Run Code Online (Sandbox Code Playgroud)
jsfiddle:http://jsfiddle.net/EcFLk/2/
但是有更简单/更短的方式吗?内联解决方案(不使用特殊功能)会很棒.谢谢.
var x = {
"Item1" : 1,
"Item2" : {
"Item3" : 3
}
}
alert(JSON.stringify(x, undefined, 2));
alert($.parseJSON(x));
Run Code Online (Sandbox Code Playgroud)
第一个警告有效对象.第二个警告null.在实际代码中,"x"变量可以是字符串或对象,因此我应该能够解析这两种类型.我错过了什么吗?
如何将类似initialArrayJSON对象的数组转换为finalObjectmap?
var initialArray = [
{ id:'id1', name:'name1' },
{ id:'id2', name:'name2' },
{ id:'id3', name:'name3' },
{ id:'id4', name:'name4' }
];
var finalObject = {
'id1':'name1',
'id2':'name2',
'id3':'name3',
'id4':'name4'
}
Run Code Online (Sandbox Code Playgroud)
需要考虑的事项:
有任何想法吗?
$('#all_locations').append("<table>");
$('#all_locations').append("<tr><th>City</th></tr>");
$.each(data, function(i, item){
$('#all_locations').append("<tr>");
$('#all_locations').append("<td>"+item.city+"</td>");
$('#all_locations').append("<tr>");
}
$('#all_locations').append("</table>");
Run Code Online (Sandbox Code Playgroud)
输出得到了使用 alert($('#all_locations').html());
<table></table>
<tr><th>City</th></tr>
<tr></tr><td>Seattle</td>
<tr></tr><td>Chicago</td>
Run Code Online (Sandbox Code Playgroud)
当ajax调用完成时,此代码将触发.任何想法为什么会这样做?
假设数据变量是有效的JSON对象.
我不知道如何最好地解释这个问题,因为有很多事情要发生,所以我继续创造了一个我正在做的样本.以下是代码:
var cl = new Class();
cl.handleAction("triggerScream");
cl.handleAction('triggerDisplay');
function Class()
{
//function which will print static string
this.scream = function(){
document.write("AAAAAAA!!!<br/>");
}
//function which will print the class variable
this.display = function(){
document.write( this.color );
};
//sample class variable
this.color = 'red';
//map of actions
this.actions = {
'triggerDisplay' : this.display,
'triggerScream' : this.scream
};
//generic function that handles all actions
this.handleAction = function(action){
try{
this.actions[action]();
}catch(err)
{
document.write("Error doing "+action);
}
};
}
Run Code Online (Sandbox Code Playgroud)
这里是jsbin链接:http://jsbin.com/etimer/2/edit
在摘要中,有一个handleAction()函数,它处理各种事件并唤起其他函数来完成事件.为此,我有动作事件和功能的地图来唤起. …
jquery ×5
javascript ×4
json ×3
append ×1
date ×1
html ×1
html-table ×1
parsing ×1
performance ×1