JS time object and reference type
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 '0': arr.push('零');break; case '1': arr.push('一');break; case '2': arr.push('二');break; case '3': arr.push('三');break; case '4': arr.push('四');break; case '5': arr.push('五');break; case '6': arr.push('六');break; case '7': arr.push('七');break; case '8': arr.push('八');break; case '9': arr.push('九');break; } } console.log(time); arr.splice(4,0,'年'); arr.splice(7,0,'月'); arr.splice(10,0,'日'); arr = arr.join(''); return arr; }//这种方法有一定缺陷,比如'2016-02-28',输出'二零一六年零二月二八日',读起来很别扭 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]] + '年' + dict[Number(month)] +'月' + dict[Number(day)] + '日'; }; getChsDate('2015-01-08');//"二零一五年一月八日"
Write a function to get the date n days ago:
var lastWeek = getLastNDays(7); // ‘2016-01-08’ var lastMonth = getLastNDays(30); //'2015-12-15'
Code:
var lastWeek = getLastNDays(7); // ‘2016-01-08’ var lastMonth = getLastNDays(30); //'2015-12-15' 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+'-'+month+'-'+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] =='Array') { 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] ==='Array'){ 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

With the popularity of short video platforms, video matrix account marketing has become an emerging marketing method. By creating and managing multiple accounts on different platforms, businesses and individuals can achieve goals such as brand promotion, fan growth, and product sales. This article will discuss how to effectively use video matrix accounts and introduce different types of video matrix accounts. 1. How to create a video matrix account? To make a good video matrix account, you need to follow the following steps: First, you must clarify what the goal of your video matrix account is, whether it is for brand communication, fan growth or product sales. Having clear goals helps develop strategies accordingly. 2. Choose a platform: Choose an appropriate short video platform based on your target audience. The current mainstream short video platforms include Douyin, Kuaishou, Huoshan Video, etc.

References and pointers in C++ are both methods of passing function parameters, but there are differences. A reference is an alias for a variable. Modifying the reference will modify the original variable, while the pointer stores the address of the variable. Modifying the pointer value will not modify the original variable. When choosing to use a reference or a pointer, you need to consider factors such as whether the original variable needs to be modified, whether a null value needs to be passed, and performance considerations.

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
