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!

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


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

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.