JS 时间对象、引用类型

基础类型有哪些?复杂类型有哪些?有什么特征?##

JS 时间对象、引用类型_第1张图片
数据类型.png

如下代码的输出? 为什么?##

var obj1 = {a:1, b:2};
var obj2 = {a:1, b:2};
console.log(obj1 == obj2);//false,二者指向的两块堆内存
console.log(obj1 = obj2);//使obj1指向obj2的堆内存
console.log(obj1 == obj2);//true,二者指向同一块堆内存

写一个函数getIntv,获取从当前时间到指定日期的间隔时间##

    function getIntv(str){
      var val = Date.parse(str) - Date.now();
      var dd = Math.floor(val/(24*60*60*1000));
      var hh = Math.floor((val-(dd*24*60*60*1000))/(60*60*1000));
      var mm = Math.floor((val-(dd*24*60*60*1000+hh*60*60*1000))/(60*1000));
      var ss = Math.floor((val-(dd*24*60*60*1000+hh*60*60*1000+mm*60*1000))/1000);
      var time = "据除夕还有 "+dd+" 天 "+hh+" 小时 "+mm+" 分 "+ss+" 秒"
      return time;
    }
    var str = getIntv("2017-01-07");
    console.log(str)//据除夕还有 172 天 16 小时 9 分 43 秒

把数字日期改成中文日期##

    function getChsDate(str){
      var date = new Date(str);
      var year = date.getFullYear().toString(),
          month = date.getMonth(),
          day = date.getDate();
      var arr=["零","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十","三十一"];
      var chsYear = "";
      for (var i = 0; i < year.length; i++) {
        chsYear += arr[year[i]];
      }
      chsDate = chsYear+"年"+arr[month + 1]+"月"+arr[day]+"日";
      return chsDate
    }
    var str = getChsDate('2016-07-18');
    console.log(str);  // 二零一六年七月十八日

写一个函数获取n天前的日期##

function getLastNDays(val){
  var lastNDays = new Date(Date.now() - val*24*60*60*1000);
  var str = lastNDays.getFullYear()+"-"+(lastNDays.getMonth()+1)+"-"+lastNDays.getDate();
  return str;
}
var lastWeek =  getLastNDays(7); // "2016-7-11"
var lastMonth = getLastNDays(30); // "2016-6-18"

获取执行时间##

var Runtime = (function(){
     var startDate,endDate;
     return {
        start: function(){
            startDate = Date.now();
        },
        end: function(){
            endDate = Date.now();
        },
        get: function(){
            return endDate - startDate;
         }
     };
}());
Runtime.start();
//todo something
Runtime.end();
console.log(  Runtime.get() );

楼梯有200级,每次走1级或是2级,从底走到顶一共有多少种走法?用代码(递归)实现##

function fn(n){
if (n===1) {
return 1;
}
if (n===2) {
return 2;
}
return fn(n-1) + fn(n-2);
}

写一个json对象深拷贝的方法,json对象可以多层嵌套,值可以是字符串、数字、布尔、json对象中的任意项##

    function objCopy(obj){
          var newObj = {};
          for (var key in obj) {
            if(typeof arr[key] === "object"){
              newObj[key] = objCopy(obj[key])
            }else{
              newObj[key] = obj[key];
            }
          }
          return newObj;
        }

你可能感兴趣的:(JS 时间对象、引用类型)