This article abstract: http://www.liaoxuefeng.com/
In the JavaScript world, everything is an object.
Some objects, however, are not the same as other objects. To distinguish the type of an object, we use the typeof
operator to get the type of the object, which always returns a string:
typeof123;//' number 'typeofNaN;//' number 'typeof ' str '; //' string ' typeof true; //' boolean ' typeof undefined; //' undefined ' typeof math.abs; span class= "comment" >//' function ' typeof null; //' object ' typeof []; //' object ' typeof {}; //' object '
Visible,,,, number
string
boolean
function
and undefined
distinct from other types. The type of special attention null
is object
Array
object
also , if we use the object--that typeof
will be indistinguishable null
, Array
and in general sense {}
.
Wrapping Object
In addition to these types, JavaScript also provides packaging objects, and the familiarity of Java's small partners must be clear int
and Integer
this ambiguous relationship.
number
, boolean
and string
both have packaging objects. Yes, in JavaScript, a string also distinguishes between string
a type and its wrapper type. The wrapper object is new
created with:
var n = new Number(123); // 123,生成了新的包装类型var b = new Boolean(true); // true,生成了新的包装类型var s = new String(‘str‘); // ‘str‘,生成了新的包装类型
Although the wrapper object looks exactly the same as the original value, the display is identical, but their type object
has changed! Therefore, the wrapper object and the original value ===
are returned with a comparison false
:
typeofNew Number (123);//' object ' new number (123) = = = 123; //false typeof new Boolean (true); //' object ' new Boolean (true) = = = true; //false typeof new string (//' object ' new String ( ' str ') = = = //false
So do not use the packaging object of the pain of idle eggs ! Especially for string
type!!!
What happens if we Number
Boolean
String
do not write when we use, and when new
?
At this point,, Number()
Boolean
and as a String()
normal function, convert any type of data to number
, boolean
and string
type (note not its wrapper type):
var n = number (' 123 ');//123, equivalent to parseint () or parsefloat ()typeof n; //' number ' var b = Boolean ( ' true '); //true typeof b; //' boolean ' var b2 = Boolean ( "false"); //true! ' False ' string conversion result to true! Because it is a non-empty string! var b3 = Boolean (//false var s = String (123.45); //' 123.45 ' typeof s;//' string '
Did you feel a big head? This is the hypnotic charm peculiar to JavaScript!
To summarize, there are a few rules to follow:
Do not new Number()
use new Boolean()
, new String()
create packaging objects;
Use parseInt()
or parseFloat()
to convert any type to number
;
The String()
method used to convert any type to string
, or directly invoke, an object toString()
;
It is not usually necessary to convert any type to boolean
re-judgment, because it can be written directly if (myVar) {...}
;
typeof
Operators can determine the,,, number
boolean
string
function
and undefined
;
Judge Array
to use Array.isArray(arr)
;
Judging null
please use myVar === null
;
Determine if a global variable exists typeof window.myVar === ‘undefined‘
;
The function internally determines whether a variable exists typeof myVar === ‘undefined‘
.
number
Object Call toString()
SyntaxError:
123.toString(); // SyntaxError
In this case, special treatment is needed:
123..toString(); // ‘123‘, 注意是两个点!(123).toString(); // ‘123‘
Date
In JavaScript, Date
objects are used to represent dates and times.
To get the current time of the system, use:
var now =New Date (); now;//Wed June 19:49:22 gmt+0800 (CST) now.getfullyear () ; //2015, Year Now.getmonth (); //5, month, note the month range is 0~11,5 represents June now.getdate (); //24, indicating number 24th now.getday (); //3, representing Wednesday now.gethours (); //19, 24-hour now.getminutes (); //49, Min now.getseconds (); //22, Seconds now.getmilliseconds (); //875, number of milliseconds now.gettime (); //1435146562875, timestamp as number representation
Note that the current time is the time that the browser obtains from the native operating system, so it is not necessarily accurate because the user can set the current time to any value.
If you want to create an object that specifies a date and time Date
, you can use:
var d = new Date(2015, 5, 19, 20, 15, 30, 123);d; // Fri Jun 19 2015 20:15:30 GMT+0800 (CST)
The month range of JavaScript is expressed in integers as 0~11, which represents 0
January, which 1
represents February ..., so to represent June, we passed in 5
!
The second way to create a specified date and time is to parse a string that conforms to the ISO 8601 format:
var d = Date.parse(‘2015-06-24T19:49:22.875+08:00‘);d; // 1435146562875
But it returns not Date
an object, but a timestamp. But with a time stamp it is easy to convert it to a Date
:
var d = new Date(1435146562875);d; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)
Time Zone
Date
The time the object represents is always displayed in 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‘,本地时间(北京时区+8:00),显示的字符串与操作系统设定的格式有关 d.toUTCString(); // ‘Wed, 24 Jun 2015 11:49:22 GMT‘,UTC时间,与本地时间相差8小时
So how do you do time zone conversions in JavaScript? In fact, as long as we pass a number
timestamp of a type, we don't care about the time zone conversion. any browser can convert a timestamp to the local time correctly .
What's a time stamp? The timestamp is an auto-increment integer that represents the moment that the GMT time zone starts at zero January 1, 1970, and the current number of milliseconds . Assuming that the time of the browser's computer is accurate, the time stamp numbers in the world , regardless of the time zone, are the same at the moment, so the timestamp can represent exactly one moment and is independent of the time zone .
So, we just need to pass the timestamp, or read the timestamp out of the database and let JavaScript automatically convert to local time.
To get the current timestamp, you can use: Timestamp to generate
if (Date.now) { alert(Date.now()); // 老版本IE没有now()方法 表示时间戳} else { alert(new Date().getTime());}
Standard object-------JavaScript