JOE个人网站

JOE个人网站,不仅仅是一个网站,更像是一个展现自我的平台,致力于让朋友们都可以
有所感触,有所收获。

对象

2017-12-19 16:12:17
九、对象的声明(有3种方法)
1、通过new
<script>
var obj=new Object();
obj.name='joe';
obj.say=function() {
document.write(this.name);
};

obj.say();
</script>

2、快速声明法(推荐)
var obj={'name':'joe','age':18,'say':function(){alert(this.age)}};
obj.say();

3、(了解)通过function 声明
<script>
function People() {
this.name='joe';
this.age=18;
this.say=function() {
alert(this.age);
}
}

var obj=new People();
obj.say();
</script>

十、内置对象
1、时间对象 Date
<script type="text/javascript" charset="utf-8">
// var odate=new Date(); 如果什么参数都没有,表示当前时间;new Date(月 日 年 小时:分:秒)
var odate=new Date();
document.write('年:'+odate.getFullYear()+'<br/>');//返回当前年份(4位数字),客户端的时间;
document.write('月:'+odate.getMonth()+'<br/>');//返回月份,但是该月份是当前月份减1(0-11)
document.write('日:'+odate.getDate()+'<br/>');//返回日期
document.write('星期:'+odate.getDay()+'<br/>');//返回星期(0-6)
document.write('小时:'+odate.getHours()+'<br/>');//返回小时数
document.write('分钟:'+odate.getMinutes()+'<br/>');//返回分钟
document.write('秒:'+odate.getSeconds()+'<br/>');//返回秒
document.write('毫秒:'+odate.getMilliseconds()+'<br/>');//返回毫秒
</script>

2、布尔对象
var ob=new Boolean();
参数为:0、0.0、空字符串、false、undefined、NaN都会转化为false;
空数组和空对象为true;

3、数学对象 Math
Math.PI //获取圆周率
Math.abs()//取绝对值
var inew=Math.abs(-5);//取绝对值
Math.ceil() //进一取整
document.write(Math.ceil(1.001));//2
Math.floor()//向下取整
document.write(Math.floor(1.9));//1
Math.max(num1,num2,...numn);//取最大值
Math.max(1,9,8,6,100,102,98)
Math.min(num1,num2,...numn);//取最小值
Math.min(1,9,8,6,-100,102,98)
Math.round();//四舍五入
Math.round(2.5)//3
Math.random();//返回一个0-1之间的随机数

4、字符串对象
indexOf
lastIndexOf
match
var str='ab11111ba';
document.write(str.match(/\d{3}/)); //正则表达式也是对象,这里不用引号包围,直接使用 /正则表达式内容/
replace
var str="ab2ba3def4ab";
document.write(str.replace(/\d/g,'-')); //正则表达式后面加上g表示匹配所有符合条件的内容 ab-ba-def-ab
split
var str="a-b-c";
document.write(str.split('-')); //通过split分隔字符串为数组
slice
substr
var str="a-b-c";
document.write(str.substr(1,3)); //-b-
toLowerCase()
toUpperCase()
var str="a-b-c";
document.write(str.toUpperCase()); //A-B-C