时间对象、引用类型

2016-08-24  本文已影响0人  __Qiao

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

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

  var obj1 = {a:1, b:2};
  var obj2 = {a:1, b:2};
  console.log(obj1 == obj2);  // false,变量obj1与变量obj2存放的对象地址不一样,即指向的对象不同
  console.log(obj1 = obj2);   // Object{a:1, b:2},将obj2所指向对象的地址赋值给obj1,此时obj1指向的对象与obj2相同
  console.log(obj1 == obj2); // true,obj1指向的对象与obj2相同

代码执行各阶段,变量储存的值变化如下图所示;

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

var str = getIntv("2016-01-08");
console.log(str); // 距除夕还有 20 天 15 小时 20 分 10 秒

function getIntv(time) {
  var now = Date.now();
  var count = Math.round(Math.abs(Date.parse(time) - now) / 1000);
  var day = Math.floor(count / (24 * 3600));
  var hours = Math.floor((count % (24 * 3600)) / 3600);
  var minutes = Math.floor(((count % (24 * 3600)) % 3600) / 60);
  var seconds = Math.floor(((count % (24 * 3600)) % 3600) % 60);
  return "距除夕还有" + day + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
}

把数字日期改成中文日期

var str = getChsDate('2015-01-08');
console.log(str); // 二零一五年一月八日

function getChsDate(time) {
  var numChsChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  var d = new Date(time);
  var year = d.getFullYear();
  var month = d.getMonth() + 1;
  var date = d.getDate();
  var yearChs ,  monthChs,  dateChs ;
  yearChs = numChsChar[Math.floor(year / 1000)] + numChsChar[Math.floor((year % 1000) / 100)] + numChsChar[Math.floor(((year % 1000) % 100) / 10)] + numChsChar[Math.floor(((year % 1000) % 100) % 10)];
  if (Math.floor(month / 10) > 0) {
    monthChs =  "十" + numChsChar[Math.floor(month % 10)];
  } else {
    monthChs = numChsChar[Math.floor(month % 10)];
  }
  if (Math.floor(date / 10) > 0) {
   if (Math.floor(date / 10) == 1){
        dateChs = "十" + numChsChar[Math.floor(date % 10)];
    }
    if (Math.floor(date / 10) >1) {
        dateChs = numChsChar[Math.floor(date / 10)] + "十" + numChsChar[Math.floor(date % 10)];
    }
  } else {
    dateChs = numChsChar[Math.floor(date % 10)];
  }
  return yearChs + "年" + monthChs + "月" + dateChs + "日";
}

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

var lastWeek = getLastNDays(7); // ‘2016-01-08’
var lastMonth = getLastNDays(30); //'2015-12-15'

function getLastNDays(n) {
  var now = new Date();
  var last = new Date(now - n * 24 * 60 * 60 *1000);
  return last.getFullYear() + '-' + (last.getMonth()+1) + '-' + last.getDate();
}

完善如下代码,用于获取执行时间如:

var Runtime = (function(){
 //code here ...
 var obj = {
 start: function(){
 //code here ..., 当前时间
 },
 end: function(){
 //code here ... 结束时间
 },
 get: function(){
 //code here ... 获取执行时间
 }
 };
return obj;
}());
Runtime.start();
//todo somethint
Runtime.end();
console.log( Runtime.get() );

var Runtime = (function(){
  var startTime, endTime;
  var obj = {
      start: function(){
        startTime = Date.now();
        return startTime;
      },
      end: function(){
        endTime = Date.now();
        return endTime;
      },
      get: function(){
         return endTime - startTime + "ms";
      }
  };
  return obj;
}());
Runtime.start();
//todo somethint
Runtime.end();
console.log(  Runtime.get() );

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


如图所视,每到一级,在走下一级时都有两种走法:

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

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

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

本文版权属吴天乔所有,转载务必注明出处。

上一篇 下一篇

猜你喜欢

热点阅读