The built-in objects in JavaScript include number, Boolean, String, Data, Array, etc. You can access more methods and properties by creating objects for them in JavaScript.
The operating environment of this article: Windows 7 system, Dell G3 computer, javascript1.8.5.
The built-in objects in JavaScript include number, Boolean, String, Data, Array, etc. You can access more methods and properties by creating objects for them
All the programming languages we learn have built-in Objects, these objects create the basic functionality of the language, so JavaScript also has many intrinsic objects that define it as a language. This article will introduce you to several built-in languages in JavaScript in detail.
Number
Create an object by setting a variable to a numeric value via Number. Then, this variable can access the properties and methods of the object. We can use it in conjunction with the new keyword
var demo=new Number();
The Number object contains four properties:
MAX_VALUE: The maximum number that can be processed
MIN_VALUE: The smallest number that can be processed
NEGATIVE_INFINITY: The largest negative number that can be processed, expressed as -Infinity
POSITIVE_INFINITY: Values greater than MAX_VALUE are expressed as Infinity
Example
var demo = Number.MAX_VALUE
In addition to storing numerical values, the Number object also includes various properties and methods for manipulating or retrieving information about the number. All properties available to Number objects are read-only constants, which means that their values always remain the same and cannot be changed.
Boolean
An object representing a true or false value, usually a variable set to a true or false value:
var demo = true;
Boolean objects include toString And the valueOf method, its combination with conditional statements provides a way to create logic using JavaScript. There is no doubt that Boolean objects are a very important part of JavaScript. Without a Boolean object, there would be nothing to evaluate in the conditional statement.
<script> var myBoolean = true; if(myBoolean == true) { console.log("这是正确的"); } else { console.log("这是错误的"); } </script>
String
The function of the String object in JavaScript is to store text. In addition, the object has various Properties and methods to manipulate information about text. Like the Boolean object, String can be used without instantiation.
var myString = "My string";
The String object has only one attribute length, which is used to return the length of the string.
var myString = "My string"; console.log("这个字符串的长度为:"+myString.length);
Data
The Date object in JavaScript provides a way to handle dates and times, and it can be instantiated in many different ways depending on the desired results
Example: When no parameters are passed
var myDate = new Date();
Pass milliseconds as a parameter:
var myDate = new Date(milliseconds);
Pass the date string as a parameter:
var myDate = new Date(dateString);
Pass multiple parameters to create a complete Date
var myDate = new Date(year, month, day, hours, minutes, seconds, milliseconds);
Example
var myDate = new Date(); console.log(myDate.getDate()) var myDate1=new Date("2018-11-28"); console.log(myDate1);
There are many methods in the Date object to convert the date into a string,
toDateString: Convert the date part of the Date object into a string
toLocaleDateString: Convert the date part of the Date object into a string according to the local time format
toLocaleTimeString: According to the local time format, convert The time portion of the Date object is converted to a string.
toLocaleString: Convert the Date object to a string according to the local time format.
toTimeString: Convert the time part of the Date object into a string.
toUTCString: Returns the number of milliseconds from January 1, 1970 to the specified date according to universal time.
Example:
var myDate = new Date(); document.write(myDate.toDateString());
Array
The Array object in JavaScript can be used to store multiple values in a variable at the same time, and it There are many ways to operate it.
var myArray = new Array(1, 2, 3); for(var i=0; i<myArray.length; i++) { console.log(myArray[i]); }
In addition, there are many methods that can be used for Array objects to add, delete, or sort them. Note that indexes are very important when dealing with arrays. Arrays are basically operated on through indexes.
Summary: The properties and methods provided by JavaScript's built-in objects are just the beginning of the functionality. JavaScript is a flexible language, and we can continue to create new custom functions based on it to make it more powerful.
The above is the detailed content of What are the built-in objects in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!