Detailed explanation of JS built-in object instances
Date date object The date object can store any date and can be accurate to milliseconds (1/1000 seconds).
Define a time object:
var Udate=new Date();
Note: When using the keyword new, the first letter of Date() must be capitalized.
Make Udate a date object, and it has an initial value: current time (current computer system time).
If you want to customize the initial value, you can use the following method:
var d = new Date(2012, 10, 1); //October 1, 2012
var d = new Date('Oct 1, 2012'); //October 1, 2012
We'd better use the "method" introduced below to strictly define the time.
Access method syntax: "
Common methods for processing time and date in Date objects:
返回/设置年份方法 get/setFullYear() 返回/设置年份,用四位数表示。 var mydate=new Date();//当前时间2014年3月6日 document.write(mydate+””); //输出当前时间 document.write(mydate.getFullYear()+””); //输出当前年份 mydate.setFullYear(81); //设置年份 document.write(mydate+””); //输出年份被设定为 0081年。
Note: Different browsers have different results of mydate.setFullYear(81). The year is set to 0081 or 81.
Result:
Thu Mar 06 2014 10:57:47 GMT+0800
2014
Thu Mar 06 0081 10:57:47 GMT+0800
Note:
1. The result format is: week, month, day, year, hour, minute, second, time zone. (Firefox)
Different browsers have different time formats.
Returning day of the week method
getDay() returns the day of the week, returning a number from 0 to 6, 0 means Sunday. If you want to return the corresponding "week", complete it through an array, the code is as follows:
var mydate=new Date();//Define the date object var weekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; //Define the array object and assign a value to each array item var mynum=mydate.getDay();//The return value is stored in the variable mynum document.write(mydate.getDay());//Output getDay() to get the value document.write("Today is: "+ weekday[mynum]);//Output the day of the week
Note: The above code was run on Friday, March 7, 2014.
Result:
5
Today is: Friday
Return/set time method
get/setTime() Return/set time in milliseconds, Calculates the number of milliseconds from 0:00 on January 1, 1970 to the date pointed to by the date object.
If you delay the time of the current date object by 1 hour, the code is as follows:
var mydate=new Date(); document.write("当前时间:"+mydate+" "); mydate.setTime(mydate.getTime() + 60 * 60 * 1000); document.write("推迟一小时时间:" + mydate);
Result:
Current time: Thu Mar 6 11:46:27 UTC+0800 2014
Delay one hour: Thu Mar 6 12:46:27 UTC+0800 2014
Note: 1. One hour 60 minutes, one minute 60 seconds, one second 1000 milliseconds
2. 时间推迟 1 小时,就是: “x.setTime(x.getTime() + 60 * 60 * 1000);”
String String object
We have used string objects in previous studies. The way to define a string is to assign a value directly. For example:
var mystr = "I love JavaScript!"
After defining the mystr string, we can access its properties and methods.
Access the property length of the string object:
stringObject.length; Returns the length of the string.
var mystr=”Hello World!”;
var myl=mystr.length;
After the above code is executed, the value of myl will be: 12
Access string object Method:
Use the toUpperCase() method of the String object to convert the string lowercase letters to uppercase:
var mystr=”Hello world!”;
var mynum=mystr. toUpperCase();
After the above code is executed, the value of mynum is: HELLO WORLD!
Return the character at the specified position
The charAt() method can return the character at the specified position. The characters returned are strings of length 1.
Syntax:
stringObject.charAt(index)
Parameter description:
Note: 1. The subscript of the first character in the string is 0. The subscript of the last character is the string length minus one (string.length-1).
2. If the parameter index is not between 0 and string.length-1, this method will return an empty string.
For example: in the string "I love JavaScript!", return the character at position 2:
<script type="text/javascript"> var mystr="I love JavaScript!" document.write(mystr.charAt(2));</script>
Note: A space is also counted as a character.
Returns the position where the specified string first appears.
The indexOf() method can return the position where a specified string value first appears in the string.
Syntax
stringObject.indexOf(substring, startpos)
Parameter description:
Description:
1. This method will retrieve the string from beginning to end stringObject to see if it contains substring substring.
2. Optional parameter, search for substring starting from the startpos position of stringObject. If there is no such parameter, it will search from the starting position of stringObject.
3. If a substring is found, return the position of the first occurrence of substring. Character positions in stringObject start from 0.
Note: 1. The indexOf() method is case-sensitive.
2. If the string value to be retrieved does not appear, this method returns -1.
For example: Perform different searches within the "I love JavaScript!" string:
var str="I love JavaScript!" document.write(str.indexOf("I") + " "); document.write(str.indexOf("v") + " "); document.write(str.indexOf("v",8));
The output of the above code:
0
4
9
Related recommendations:
js built-in object learning notes_javascript skills
The above is the detailed content of Detailed explanation of JS built-in object instances. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


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

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment