問答
-
基礎(chǔ)類型有哪些脊另?復(fù)雜類型有哪些?有什么特征约巷?
答:
- 基礎(chǔ)類型:
- 數(shù)字number
- 字符串string
- 布爾值boolean
- null
- undefined
- 復(fù)雜類型:
- 對象object
- 特征:
- 基礎(chǔ)類型:變量儲存的是值偎痛。
- 復(fù)雜類型:變量儲存的是內(nèi)存地址。
-
基礎(chǔ)類型再賦值的時(shí)候拷貝的是值独郎,復(fù)雜函數(shù)則是地址踩麦。
- 基礎(chǔ)類型:
-
如下代碼的輸出? 為什么?
var obj1 = {a:1, b:2}; var obj2 = {a:1, b:2}; console.log(obj1 == obj2);//輸出false枚赡,因?yàn)閛bj1和obj2的地址不同。 console.log(obj1 = obj2);//輸出obj2{a:1, b:2}谓谦,將obj2地址給了obj1贫橙。 console.log(obj1 == obj2);//輸出true,因?yàn)楝F(xiàn)在obj1和obj2指向的地址相同反粥。
代碼題
-
寫一個(gè)函數(shù)getIntv卢肃,獲取從當(dāng)前時(shí)間到指定日期的間隔時(shí)間
function getIntv (str) { var nowtime = Date.now(); var fut = Date.parse(str); var dtime = Math.abs(fut-nowtime); var day = Math.floor(dtime/(24*3600000)); var hour = Math.floor((dtime-day*24*3600000)/3600000); var min = Math.floor((dtime-(day*24+hour)*3600000)/60000); var sec = Math.floor((dtime-(day*24+hour)*3600000-min*60000)/1000); var result; if(fut>nowtime){ result = "距離"+ str + "還有:"+ day+"天"+ hour+"時(shí)"+ min +"分"+ sec +"秒"; } else { result = "距離"+ str + "已過去了:"+ day+"天"+ hour+"時(shí)"+ min +"分"+ sec +"秒"; } return result; } var str = getIntv("2017-01-08"); console.log(str); var str = getIntv("2016-01-08"); console.log(str);
-
把數(shù)字日期改成中文日期
var str = getChsDate('2015-01-08'); console.log(str); // 二零一五年一月八日
function getChsDate(vl){ var a =['零','一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五','十六','十七','十八','十九','二十','二十一','二十二','二十三','二十四','二十五','二十六','二十七','二十八','二十九','三十','三十一']; var t = new Date(vl); var year = t.getFullYear()+''; var month = t.getMonth()+1+''; var date = t.getDate()+''; var n = ''; for(var i =0;i<year.length;i++){ n = n+a[year[i]]; } return n+'年'+a[month]+'月'+a[date]+'日'; } var str = getChsDate('2015-01-08'); console.log(str); // 二零一五年一月八日
寫一個(gè)函數(shù)獲取n天前的日期
var lastWeek = getLastNDays(7); // ‘2016-01-08’
var lastMonth = getLastNDays(30); //'2015-12-15'
```
```
var lastWeek = getLastNDays(7); // ‘2016-01-08’
var lastMonth = getLastNDays(30); //'2015-12-15'
function getLastNDays(num){
var t = new Date();
var t2 = new Date(t.getTime()-num*24*3600*1000);
return t2.getFullYear()+'-'+(t2.getMonth()+1)+'-'+t2.getDate();
}
```
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2858982-d116d3a4b0c54be4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4. 完善如下代碼,用于獲取執(zhí)行時(shí)間如:
```
var Runtime = (function(){
//code here ...
var obj = {
start: function(){
//code here ...才顿, 當(dāng)前時(shí)間
},
end: function(){
//code here ... 結(jié)束時(shí)間
},
get: function(){
//code here ... 獲取執(zhí)行時(shí)間
}
};
return obj;
}());
Runtime.start();
//todo somethint
Runtime.end();
console.log( Runtime.get() );
```
```
var Runtime = (function() {
//code here ...
var t1, t2, t3;
var obj = {
start: function() {
//code here ...莫湘, 當(dāng)前時(shí)間
t1 = Date.now();
},
end: function() {
//code here ... 結(jié)束時(shí)間
t2 = Date.now();
},
get: function() {
//code here ... 獲取執(zhí)行時(shí)間
t3 = t2 - t1;
return t3;
}
};
return obj;
}());
Runtime.start();
//todo somethint
for(var i = 0;i<10000;i++){
console.log(1);
}
Runtime.end();
console.log(Runtime.get());
```
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2858982-ebe35f8d9372c3e9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
5. 樓梯有20級,每次走1級或是2級郑气,從底走到頂一共有多少種走法幅垮?用代碼(遞歸)實(shí)現(xiàn)
```
function f(num){
if(num === 1){
return 1;
}else if(num === 2){
return 2
}else{
return f(num-2)+f(num-1);
}
}
```
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2858982-2adf41f5d495961d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6. 寫一個(gè)json對象深拷貝的方法,json對象可以多層嵌套尾组,值可以是字符串忙芒、數(shù)字、布爾讳侨、json對象中的任意項(xiàng)(PS:嘗試另外一種方法 var obj2 = JSON.parse( JSON.stringify(obj1) )
```
var me = {
"name": "oxc",
"age": 23,
"hobby": {
"food": "meat",
"sport": "bicycle",
"number": 24
}
};
function dc(obj1){
var obj2 = JSON.parse(JSON.stringify(obj1));
/* 先把obj1指向的對象轉(zhuǎn)化為字符串呵萨,此時(shí)obj1指向的對象不變,在把obj1字符串轉(zhuǎn)化為一個(gè)對象跨跨,這個(gè)對象和obj1指向的對象不是同一個(gè) */
return obj2;
}
```
```
var me = {
"name": "oxc",
"age": 23,
"hobby": {
"food": "meat",
"sport": "bicycle",
"number": 24
}
}
function dc(obj1){
var obj2 = {};
for(var key in obj1){
if(typeof obj1[key] == 'object'){
obj2[key] = dc(obj1[key])
}else{
obj2[key] = obj1[key]
}
};
return obj2;
}
```