var now = new Date(); //返回当前时间 var sometime1 = new Date(1465999453000); //1970年后的毫秒数 Wed Jun 15 2016 22:04:13 GMT+0800 var sometime2 = new Date("5/5/2016"); //等价于 var sometime2 = new Date(Date.parse(""5/5/2016"")); /* * Date.UTC()的参数分别是年份,月份(0-11),日(1-31),小时(0-23),分钟,秒,毫秒,而且为GTM时间 * 前两个参数是必须的,没有天则假的为1,其余假定为0 * */ var sometime3 = new Date(Date.UTC(2016,0)); // Fri Jan 01 2016 08:00:00 GMT+0800 (中国标准时间) 转换为GTM+8 var sometime4 = new Date(Date.UTC(2016,6,6,21,4,45)); //VM654:4 Thu Jul 07 2016 05:04:45 GMT+0800 (中国标准时间) //和上面两个的区别就是,上面是GTM时间,下面是本地时间。 var sometime3 = new Date(2016,0); var sometime4 = new Date(2016,6,6,21,4,45);
var someDate1 = new Date("6/31/2016"); //Fri Jul 01 2016 00:00:00 GMT+0800 Chrome 51结果 var someDate2 = new Date("Sat May 25 2016 15:20:45 GMT+0800"); //Wed May 25 2016 15:20:45 GMT+0800 (中国标准时间) chrome 51结果,星期错误
var someDate1 = new Date("5/5/2016"); var someDate2 = new Date("May 12,2016"); var someDate3 = new Date("Wed May 25 2016 15:20:45 GMT+0800"); var someDate4 = new Date("2016-05-13T13:34:45");
JavaScript中没有块级做用域,只有函数做用域。javascript
//函数做用域 var color = "blue"; function changeColor(){ var anotherColor = "red"; function swapColors(){ var tempColor = anotherColor; var anotherColor = color; color = tempColor; } swapColors(); } changeColor(); //块做用域不起做用 if(true){ var color = "blue"; } alert(color); //blue
改变做用域链html
关于做用域链,有兴趣能够参考这篇博客java
var expression = / pattern / flags ;
,flags常见的有g全局,i不区分大小写,m多行模式,注意理解多行模式。(
)
[
]
{
}
\
^
$
|
?
*
+
.
/\[bc\]at/
等于\\[bc\\]at
,这两个都匹配"[bc]at"//声明式 function sum (num1,num2){ return num1+num2; } //表达式 var sum = function(num1,num2){ return num1+num2; } //使用构造函数 var sum = new Function("num1","num2","return num1+num2"); //不推荐使用
TypeError: sayHi is not a function
:pensive: window.color = "red"; var o = { color:"blue" }; function sayColor(){ alert(this.color); } sayColor(this); //red sayColor(window); //red sayColor(o); //blue
window.color = "red"; var o = { color:"blue" }; function sayColor(){ alert(this.color); } var objectSayColor = sayColor.bind(o); objectSayColor(); //blue
var falseObject = new Boolean(false); var result = falseObject && true; alert(result); //true alert(typeof falseObject); //object
var numberObject = new Number(10); var numberValue = 10; alert(typeof numberObject); //"object" alert(typeof numberValue); //'number" alert(numberObject instanceof Number); //true alert(numberValue instanceof Number); //false