Home  >  Article  >  Web Front-end  >  JS time object and reference type

JS time object and reference type

php中世界最好的语言
php中世界最好的语言Original
2018-03-08 14:45:581608browse

This time I will bring you JS time objects and reference types. What are the precautions when using JS time objects and reference types? Here are practical cases, let’s take a look.

What are the basic types? What are the complex types? What are the characteristics?
Basic types: String type, Null type, Number type, Undefined type, Boolean type
Complex type: Object type
Function:
String type: String is a sequence of Unicode characters, commonly known as String , can be expressed by double quotes or single quotes, there is no difference, just match
Null type: The Null type has only one value: null, which represents a null pointer, that is, something that does not exist
Number Type: JavaScript’s number type is different from other languages. There is no difference between integers and floating point numbers. They are all Number types, which can represent decimal, octal, and hexadecimal
Undefined types: The Undefined type also has only one value, undefined, which means that the variable has only been declared and not initialized, that is, there is this pointer, but this pointer does not point to any space
Boolean type: Boolean has two values: 1.true2.false
Object class type: Object (object) is the core concept of JavaScript and the most important data type. All data in JavaScript can be regarded as objects, which is why we often say that everything is an object.

The output of the following code? Why?

var obj1 = {a:1, b:2};
var obj2 = {a:1, b:2};
console.log(obj1 == obj2);//false,由于obj1与obj2所储存的位置不同,所以false。
console.log(obj1 = obj2);//obj2赋值给obj1 输出 Object { a=1,  b=2} 内容。
console.log(obj1 == obj2);//把obj2赋值给obj1,所以obj2与obj1存储的位置是一样的,所以为true。

Code

Write a function getIntv to get the interval from the current time to the specified date.
var str = getIntv("2016-01-08");
console.log(str); // There are still 20 days, 15 hours, 20 minutes and 10 seconds until New Year's Eve
Code:

var str = getIntv("2017-01-27");
function getIntv(time){
var end = new Date(time);
var now = new Date();
var timer = end-now;
var day = Math.floor(timer/(1000606024));
var timer1 = timer%(1000606024)
var hour = Math.floor(timer1/(10006060));
var timer2 = timer1%(10006060);
var min = Math.floor(timer2/(100060));
var timer3 = timer2%(100060);
var sec = Math.floor(timer3/1000);
return ("距"+time+"还有"+day+"天"+hour+"小时"+min+"分钟"+sec+"秒")
}
console.log(str);  // 距2017-01-27还有 20 天 15 小时 20 分 10 秒

Change the digital date to Chinese date, such as:

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

Code:
Method one:

var str = getChsDate('2015-01-08');
function getChsDate(time){
time = time.replace(/-/g,'');
var arr = []
for(i=0;i<time.length;i++){
switch(time[i]){
case &#39;0&#39;: arr.push(&#39;零&#39;);break;
case &#39;1&#39;: arr.push(&#39;一&#39;);break;
case &#39;2&#39;: arr.push(&#39;二&#39;);break;
case &#39;3&#39;: arr.push(&#39;三&#39;);break;
case &#39;4&#39;: arr.push(&#39;四&#39;);break;
case &#39;5&#39;: arr.push(&#39;五&#39;);break;
case &#39;6&#39;: arr.push(&#39;六&#39;);break;
case &#39;7&#39;: arr.push(&#39;七&#39;);break;
case &#39;8&#39;: arr.push(&#39;八&#39;);break;
case &#39;9&#39;: arr.push(&#39;九&#39;);break;
}
}
console.log(time);
arr.splice(4,0,&#39;年&#39;);
arr.splice(7,0,&#39;月&#39;);
arr.splice(10,0,&#39;日&#39;);
arr = arr.join(&#39;&#39;);
return arr;
}//这种方法有一定缺陷,比如&#39;2016-02-28&#39;,输出&#39;二零一六年零二月二八日&#39;,读起来很别扭
console.log(str);  // 二零一五年一月八日

Method two:

function getChsDate(date){
var newDate =date.split("-"),
year = newDate[0],
month = newDate[1],
day = newDate[2];
var dict ={"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 dict[year[0]]+dict[year[1]]+dict[year[2]]+dict[year[3]] + &#39;年&#39; + dict[Number(month)] +&#39;月&#39; + dict[Number(day)] + &#39;日&#39;;
};
getChsDate(&#39;2015-01-08&#39;);//"二零一五年一月八日"

Write a function to get the date n days ago:

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

Code:

var lastWeek =  getLastNDays(7); // ‘2016-01-08’
var lastMonth = getLastNDays(30); //&#39;2015-12-15&#39;
function getLastNDays(dater){
var now = Date.now();
var timer = dater2460601000;
var past = new Date(now - timer);
var year = past.getFullYear();
var month = past.getMonth()+1;//月份从0开始算;
var day = past.getDate();
return year+&#39;-&#39;+month+&#39;-&#39;+day;
}
console.log(lastWeek);
console.log(lastMonth);

Complete the following code, such as:

var Runtime = (function(){
//code here ...
return {
start: function(){
//code here ...
},
end: function(){
//code here ...
},
get: function(){
//code here ...
}
};
}());
Runtime.start();
//todo somethint
Runtime.end();
console.log(  Runtime.get() );

Code:

var Runtime = (function(){
var time1;
var time2;
return {
start: function(){
time1=Date.now();
},
end: function(){
time2=Date.now();
},
get: function(){
return (time2-time1);
}
};
}());
Runtime.start();
for(var i=0;i<100;i++){
console.log(1);//输出100次1
}
Runtime.end();
console.log(  Runtime.get() );//运行了22ms

There are 200 steps in the stairs. Each time you take 1 or 2 levels, how many ways are there in total from the bottom to the top? Use code (recursively) to implement

function fn(num) {
if (num==0||num==1) {
return 1;
}
else {
return fn(num-1)+fn(num-2);
}
}
console.log(fn(200));

Writing a method of deep copying a json object. The json object can be nested in multiple layers, and the value can be a string, number, Boolean, or any item in the json object

var json={
"name":"yahoo",
"age":"14",
"sex":"man",
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
}
}
function JSON(arr){
var newjson= {};
for(key in arr){
if(typeof arr[key]=="object"){
newjson[key]=JSON(arr[key]);
}
else{
newjson[key]=arr[key];
}
}
return newjson;
}
console.log(JSON(json))

Write a deep copy method of an array. The values ​​in the array can be strings, numbers, Boolean, or any items in the array.

var arr=[1,"2",3,[1,2,3,4],true]
function JSON(arr){
var newarr=[];
for(key in arr){
if(typeof arr[key] ==&#39;Array&#39;) {
newarr[key]=JSON(arr[key]);
}
else{
newarr[key]=arr[key];
}
}
return newarr;
}
console.log(JSON(arr))

Write a deep copy method Method, copy object and internal nested value can be any item in string, number, Boolean, array, json object

var O={
name:"yahoo",
age:14,
other:[1,2,true,"yahoo",3],
man:{
"man1":"woman",
"man2":"man2"
},
aid:true,
address:
{
streetAddress: "21 2nd Street",
city: "New York",
state: "NY",
postalCode: "10021"
}
}
function JOSN(O){
var newarr={};
for(key in O){
if (typeof O[key] ===&#39;Array&#39;){
newarr[key]=JOSN(O[key]);
}
else{
newarr[key]=O[key];
}
}
return newarr;
}
console.log(JOSN(O))

I believe you have mastered the method after reading the case in this article, more Please pay attention to other related articles on the php Chinese website!

Related reading:

Simple bubble and two-way bubble sorting case

Javascript used to download images script

The above is the detailed content of JS time object and reference type. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:jquery's DOM and eventsNext article:jquery's DOM and events