javascript数据类型中的基础类型有哪些?复杂类型有哪些?有什么特征?
- 基本类型有number,boolean,string,null,undefined,它们的值存储在一块连续的栈内存中,单个数据占用内存较小。
- 复杂类型有:object,Array,函数,正则,它们的值存储在堆内存中,变量中保存的只是一个指针,由指针指向存储值的堆内存。
如下代码的输出? 为什么?
1 | var obj1 = {a:1, b:2}; |
输出:1.false:因为obj1和obj2的类型书对象,一引用类型,里面存储的是一个指向存储在一块区域(堆内存)的指针,这两个存储的指针是不同的,所以 console.log(obj1 == obj2)输出false
2.Object {a: 1, b: 2}:第二句obj1=obj2是一个赋值语句,即把obj2的指针赋值给boj1,然后输出obj1,即obj1指针指向的内容:Object {a: 1, b: 2}
3.true:obj1里面存储的指针是obj2的指针,这两个变量名的内容是相等的,输出true。
代码
写一个函数getIntv,获取从当前时间到指定日期的间隔时间
1
2
3
4
5
6
7
8
9
10
11function getIntv (date) {
var inter = Date.parse(date) - Date.now();
var days = Math.floor(inter/(24*60*60*1000));
var hours = Math.floor((inter%(24*60*60*1000))/(60*60*1000));
var minutes = Math.floor(((inter%(24*60*60*1000))%(60*60*1000))/(60*1000));
var seconds = Math.floor((((inter%(24*60*60*1000))%(60*60*1000))%(60*1000))/1000);
return "距除夕还有 "+ days + " 天 " + hours +" 小时 " + minutes + " 分 " + seconds + " 秒";
}
var str = getIntv("2017-01-27");
console.log(str); // 距除夕还有 250 天 17 小时 38 分 50 秒把数字日期改成中文日期
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18function getChsDate (str) {
var arr = str.split("-");
var year = arr[0];
var month = arr[1];
var day = arr[2];
var switchDate = {"0":"零","1": "一", "2": "二", "3": "三","4": "四",
"5": "五","6": "六","7": "七", "8": "八", "9": "九",
"10": "十", "11": "十一", "12": "十二","13": "十三",
"14": "十四", "15": "十五", "16": "十六", "17": "十七",
"18": "十八", "19": "十九","20": "二十","21": "二十一",
"22": "二十二", "23": "二十三", "24": "二十四", "25": "二十五",
"26": "二十六", "27": "二十七", "28": "二十八", "29": "二十九",
"30": "三十", "31": "三十一"};
return switchDate[year[0]] + switchDate[year[1]] + switchDate[year[2]] + switchDate[year[3]] + "年" + switchDate[Number(month)] +"月"+ switchDate[Number(day)] +"日";
}
var str = getChsDate('2015-01-08');
console.log(str); // 二零一五年一月八日
修改后(上面json对象的方式明显复杂了):1
2
3
4
5
6
7
8
9
10
11
12
13
14function getChsDate (str) {
var arr = str.split("-");
var year = arr[0];
var month = arr[1];
var day = arr[2];
var switchDate = ["零","一","二","三", "四","五", "六", "七", "八", "九",
"十", "十一", "十二","十三", "十四", "十五", "十六", "十七",
"十八", "十九", "二十", "二十一", "二十二", "二十三","二十四",
"二十五","二十六","二十七","二十八","二十九","三十","三十一"];
return switchDate[year[0]] + switchDate[year[1]] + switchDate[year[2]] + switchDate[year[3]] + "年" + switchDate[Number(month)] +"月"+ switchDate[Number(day)] +"日";
}
var str = getChsDate('2016-03-08');
console.log(str);
写一个函数获取n天前的日期
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15function getLastNDays (n) {
var during = n*24*60*60*1000,
lastDay = new Date(Date.now()-during),
year = lastDay.getFullYear(),
day = lastDay.getDate(),
month;
if (lastDay.getMonth()<9) {
month = "0"+(lastDay.getMonth()+1);
}else{
month = (lastDay.getMonth()+1);
};
return year +"-"+ month +"-"+ day;
}
var lastWeek = getLastNDays(7); // ‘2016-05-13’
var lastMonth = getLastNDays(30); //'2015-04-20'完善如下代码,用于获取执行时间如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24var Runtime = (function(){
return {
start: function(){
time1 = Date.now(); //code here ..., 当前时间
},
end: function(){
time2 = Date.now();//code here ... 结束时间
},
get: function(){
return (time2-time1);//code here ... 获取执行时间
}
};
}());
Runtime.start();
for (var i = 0; i < 10000; i++) {
var j=4+5;
console.log(j);
};
Runtime.end();
console.log(Runtime.get());//5332ms楼梯有200级,每次走1级或是2级,从底走到顶一共有多少种走法?用代码(递归)实现
1
2
3
4
5
6
7
8
9
10
11function countSteps(num){
if(num === 1){
return 1;
}
if(num === 2){
return 2;
}
return countSteps(num-1)+countSteps(num-2);
};
console.log(countSteps(5));//200级数量太大,浏览器被卡住写一个json对象深拷贝的方法,json对象可以多层嵌套,值可以是字符串、数字、布尔、json对象中的任意项
1
2
3
4
5
6
7
8
9
10
11
12
13function jsonCopy(json){
var newJson = {};
for (var k in json) {
if (typeof json[k] === 'object') {
newJson[k] = jsonCopy(json[k]);
}
newJson[k] = json[k];
}
return newJson;
}
var json = {"name":"word","info":{"age":10,"sex":"男","favorite":"walk"}};
console.log(jsonCopy(json));写一个数组深拷贝的方法,数组里的值可以是字符串、数字、布尔、数组中的任意项目
1
2
3
4
5
6
7
8
9
10
11
12
13function arrCopy (arr) {
var newArr = [];
for(k in arr){
if (typeof arr[k] === "object") {
newArr = arrCopy(arr[k]);
};
newArr[k] = arr[k];
}
return newArr;
}
var arr = ["djhgjb",890,["dfe",77,"0k0"],true];
console.log(arrCopy(arr));写一个深拷贝的方法,拷贝对象以及内部嵌套的值可以是字符串、数字、布尔、数组、json对象中的任意项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15function deepCopy (obj) {
var newObj = {};
for (k in obj) {
if(typeof obj[k] === "object"){
newObj[k] = deepCopy(obj[k]);
};
newObj[k] = obj[k];
};
return newObj;
}
var obj = ["string",["arr",950,["arr","true",'0994']],
{"name":"word","info":{"age":10,"sex":"男","favorite":"walk"}}
];
console.log(deepCopy(obj));