javascript对象的一些基本的方法

Date对象
Date() 返回当日的日期和时间。
getDate() 方法可返回月份的某一天。
getFullYear() 从 Date 对象以四位数字返回年份。
getTime() 返回 1970 年 1 月 1 日至今的毫秒数。
getTime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数。

比如返回1970 年 1 月 1 日距当前博客书写时间之间的毫秒数:
var a=new Date();
document.write(a.getTime()+ " milliseconds since 1970/01/01");
1539099338855 milliseconds since 1970/01/01

var b=new Date("August 12,1998 23:12:01");
document.write(b.getTime()+ " milliseconds since 1970/01/01")
902934721000 milliseconds since 1970/01/01

setDate() 方法用于设置一个月的某一天。
setMonth() 设置 Date 对象中月份 (0 ~ 11)。
setFullYear() 设置 Date 对象中的年份(四位数字)。
toString() 把 Date 对象转换为字符串

Math()对象
Math.abs() 可返回数的绝对值
Math.floor(x) 小于等于 x,且与 x 最接近的整数。

document.write(Math.floor(-5.1) + "<br />")
结果为-6

max(x,y) 返回 x 和 y 中的最高值。
min(x,y) 返回 x 和 y 中的最低值。
Math.pow(x,y) pow() 方法可返回 x 的 y 次幂的值。
Math.random() 可返回介于 0 ~ 1 之间的一个随机数。
Math.round(x) round() 方法可把一个数字四舍五入为最接近的整数
sin(x) 返回数的正弦。
cos(x) 返回数的余弦
tan(x) 返回角的正切
sqrt(x) 返回数的平方根。

JavaScript 全局对象(Global)
全局属性和函数可用于所有内建的 JavaScript 对象。
isNaN() (Is Not A Number)函数用于检查其参数是否是非数字值。
如果 x 是特殊的非数字值 NaN(或者能被转换为这样的值),返回的值就是 true。如果 x 是其他值,则返回 false。

示例:document.write(isNaN(123)+ "<br />")
document.write(isNaN(-1.23)+ "<br />")
document.write(isNaN(0)+ "<br />")
document.write(isNaN("Hello")+ "<br />")
false
false
false
true

Number() 把对象的值转换为数字。
parseFloat() 解析一个字符串并返回一个浮点数。
parseInt() 解析一个字符串并返回一个整数。
String() 把对象的值转换为字符串。

猜你喜欢

转载自blog.csdn.net/qq_41611820/article/details/82981107