您的位置:首页 > 编程语言 > Java开发

java内置核心语言对象

2016-04-16 15:03 561 查看
内置核心对象

object string array date Math

一:对象

<script>
//对象的创建

//方法一:
var people=new Object();
alert (people);						//[object Object]
alert (typeof(people));					//object
alert ( people instanceof Object);		//true  :相当于people是继承与Object

//方法二:

var people =new Object;
alert (people);						//[object Object]
alert (typeof(people));					//object
alert ( people instanceof Object);		//true  :相当于people是继承与Object

//方法三:

var people ={
//键:值
name:"张三"
}
alert (people);						//[object Object]
alert (typeof(people));					//object
alert ( people instanceof Object);		//true  :相当于people是继承与Object

//方法四:
var people	= function(){

}
//万物皆对象
var boy= new people();
alert (people);						// function(){}
alert (typeof(people));					//function
alert ( people instanceof Object);		//true  :相当于boy是继承与Object

//对象的使用

//一:
var o=new Object;
o.name="shuang";
o["sex"]="man";
alert (  o.name +"   "+ o.sex );//shuang  man

//二
var o1={
name:'shuang',
sex:'man'
}
for( var i in o1){
alert (i+":"+o1[i]);	//name :shuang   sex:  man
}
alert(o1.hasOwnProperty("name"));//true

</script>

二:数组

var arr=new Array(数组长度);

var arr=["","",""];

arr.sort();数组的排序

arr.join("-");链接

arr.push();在数组的最后一个位置添加,返回长度

arr.shift();在数组的第一个位置移除 返回该移除的元素

arr.pop();在数组的最后一个位置移除 返回该移除的元素

arr.unshift();在第一个位置添加;返回长度

Math.random();[0-1)

Math.floor( 数字 );//向下取整

Math.ceil( 数字 );//向上取整三

三:String

var str=new String()
var str="";


使用:

var str=document.title;
function line(){
str=str.substr(1,str.length)+str.substring(0,1);//substr:从1开始截取,截取str.length长度,不够就截取到最后一位;
<span style="white-space:pre">								</span>//substring:从0开始截取截取到1,包括0不包括1【0.1)
document.title=str;
console.log(str);
}
window.setInterval("line()",500);


四:Math

Math.random() [0-1)

Math.floor( int ); 向下取整

Math.ceil( int ); 向上取整

Math.round( int ); 四舍五入

使用示例1:

<script>
var r,g,b;
function color(){
r=parseInt(Math.random()*256-1);
g=parseInt(Math.random()*256-1);
b=parseInt(Math.random()*256-1);
return "rgb("+r+","+g+","+b+")";
}
function RandomColor(){
document.getElementById("s1").style.color=color();
document.getElementById("s2").style.color=color();
document.getElementById("s3").style.color=color();
}
window.setInterval("RandomColor()",300);
</script>
五:date

var date=new Date();

date.getFullyear(); 获取年份

date.getMonth(); 月份是从0开始的

date.getDate();

date.getDay(); 星期,从星期天开始 0

使用实例:

<script>
var arr=new Array(7);
arr[0]="星期天";
arr[1]="星期一";
arr[2]="星期二";
arr[3]="星期三";
arr[4]="星期四";
arr[5]="星期五";
arr[6]="星期六";
function date(){

var date=new Date();

year=date.getFullYear();
month=date.getMonth()+1;
dat=date.getDate();
day=date.getDay();
hour=date.getHours();
minu=date.getMinutes();

sec=date.getSeconds();

document.getElementById("a").innerHTML=year+"年"+month+"月"+dat+"日"+arr[day]+hour+":"+minu+":"+sec;
}
window.setInterval("date()",1000);//定时器,定时执行一个函数

</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: