In the world of JavaScript, everything is an object.
But some objects are still different from others. To distinguish the type of an object, we use typeof operator to get the type of the object, which always returns a string:
typeof 123; // 'number'typeof NaN; // 'number'typeof 'str'; // 'string'typeof true; // 'boolean'typeof undefined; // 'undefined'typeof Math.abs; // 'function'typeof null; // 'object'typeof []; // 'object'typeof {}; // 'object' typeof {}; // 'object' As can be seen, number , string , boolean , function and undefined are different from other types. Pay special attention to the type of null is object , and the type of Array is also object . If we use typeof , we will not be able to distinguish null , Array and object in the usual sense - {} .
Packaging object
number , boolean and string have wrapper objects. Yes, in JavaScript, strings also distinguish between string types and its wrapper types. The wrapping object is created with new :
var n = new Number(123); // 123, a new packaging type was generated var b = new Boolean(true); // true, a new packaging type was generated var s = new String('str'); // 'str', a new packaging type was generated Although the wrapper object looks exactly the same as the original value and is displayed exactly the same, their type has become object ! Therefore, comparing the wrapping object with the original value with === will return false :
typeof new Number(123); // 'object'new Number(123) === 123; // falsetypeof new Boolean(true); // 'object'new Boolean(true) === true; // falsetypeof new String('str'); // 'object'new String('str') === 'str'; // falseSo don’t use the packaging object even if you feel idle! Especially for string types! ! !
Date
In JavaScript, the Date object is used to represent dates and times.
To get the current time of the system, use:
var now = new Date();now; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)now.getFullYear(); // 2015, year now.getMonth(); // 5, month, note that the month range is 0~11, 5 means June now.getDate(); // 24, means 24th now.getDay(); // 3, means Wednesday now.getHours(); // 19, 24-hour now.getMinutes(); // 49, minute now.getSeconds(); // 22, seconds now.getMilliseconds(); // 875, milliseconds now.getTime(); // 1435146562875, timestamp represented in number
Note that the current time is the time the browser gets from the local operating system, so it is not necessarily accurate, because the user can set the current time to any value.
If you want to create a Date object with a specified date and time, you can use:
var d = new Date(2015, 5, 19, 20, 15, 30, 123);
You may have observed a very, very scammed, that is, the month range of JavaScript is represented by integers, 0 to 11, 0 to January, and 1 to February..., so to represent June, we are passing in 5! This is definitely something that the designer of JavaScript had a brain twitch at that time, but it is impossible to fix it now.
The second way to create a specified date and time is to parse a string that conforms to ISO 8601 format:
var d = Date.parse('2015-06-24T19:49:22.875+08:00');d; // 1435146562875 But it returns not a Date object, but a timestamp. But with a timestamp, it can be easily converted to a Date :
var d = new Date(1435146562875);d; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)
Time zone
The time represented by Date object is always displayed according to the browser's time zone, but we can display both the local time and the adjusted UTC time:
var d = new Date(1435146562875);d.toLocaleString(); // '2015/6/24 7:49:22 pm', local time (Beijing time zone +8:00), the displayed string is related to the format set by the operating system d.toUTCString(); // 'Wed, 24 Jun 2015 11:49:22 GMT', UTC time, 8 hours apart from local time
So how to convert time zones in JavaScript? In fact, as long as we are passing a number type timestamp, we don't have to care about time zone conversion. Any browser can correctly convert a timestamp to local time.
So, we only need to pass the timestamp, or read the timestamp from the database, and then automatically convert JavaScript to local time.
To get the current timestamp, you can use:
if (Date.now) { alert(Date.now()); // Old version of IE does not have the now() method} else { alert(new Date().getTime());}JSON
In JSON, there are only a few data types:
•number: It is exactly the same as JavaScript number;
•boolean: It is true or false of JavaScript;
•string: is JavaScript string;
•null: is null of JavaScript;
•array: It is the JavaScript Array representation method - [];
•object: It is the { ... } representation of JavaScript.
and any combination above.
Serialization
Let's first serialize the Xiao Ming object into a JSON string:
var xiaoming = { name: 'Xiao Ming', age: 14, gender: true, height: 1.65, grade: null, 'middle-school': '/"W3C/" Middle School', skills: ['JavaScript', 'Java', 'Python', 'Lisp']};使用JSON.stringify()之后:
JSON.stringify(xiaoming); // '{"name":"Xiao Ming","age":14,"gender":true,"height":1.65,"grade":null,"middle-school":"/"W3C/" Middle School","skills":["JavaScript","Java","Python","Lisp"]}'To output better, you can add parameters and indent the output:
JSON.stringify(xiaoming, null, ' ');
result:
{ "name": "Xiao Ming", "age": 14, "gender": true, "height": 1.65, "grade": null, "middle-school": "/"W3C/" Middle School", "skills": [ "JavaScript", "Java", "Python", "Lisp" ]} The second parameter is used to control how to filter the key values of the object. If we only want to output the specified attribute, we can pass it into Array :
JSON.stringify(xiaoming, ['name', 'skills'], ' ');
result:
{ "name": "Xiao Ming", "skills": [ "JavaScript", "Java", "Python", "Lisp" ]}You can also pass in a function so that each key-value pair of the object will be processed first by the function:
function convert(key, value) { if (typeof value === 'string') { return value.toUpperCase(); } return value;}JSON.stringify(xiaoming, convert, ' ');The above code turns all attribute values into capitalization:
{ "name": "Xiao Ming", "age": 14, "gender": true, "height": 1.65, "grade": null, "middle-school": "/"W3C/" MIDDLE SCHOOL", "skills": [ "JAVASCRIPT", "JAVA", "PYTHON", "LISP" ]} If we also want to precisely control how to serialize Xiao Ming, we can define a toJSON() method for xiaoming and directly return the data that JSON should serialize:
var xiaoming = { name: 'Xiao Ming', age: 14, gender: true, height: 1.65, grade: null, 'middle-school': '/"W3C/" Middle School', skills: ['JavaScript', 'Java', 'Python', 'Lisp'], toJSON: function () { return { // Only name and age are output, and the key has been changed: 'Name': this.name, 'Age': this.age }; }};JSON.stringify(xiaoming); // '{"Name":"Xiao Ming","Age":14}'Deserialization
Get a JSON format string, and we directly use JSON.parse() to turn it into a JavaScript object:
JSON.parse('[1,2,3,true]'); // [1, 2, 3, true]JSON.parse('{"name":"Xiao Ming","age":14}'); // Object {name: 'Xiao Ming', age: 14}JSON.parse('true'); // trueJSON.parse('123.45'); // 123.45 JSON.parse() can also receive a function to convert parsed properties:
JSON.parse('{"name":"Xiao Ming","age":14}', function (key, value) { // Put number * 2: if (key === 'name') { return value + 'Student'; } return value;}); // Object {name: 'Student Xiao Ming', age: 14}The above article briefly talks about the standard objects of JavaScript is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.