您的位置:首页 > Web前端 > JavaScript

JavaScript 内建对象Array、String、Math及自定义对象的使用

2017-12-21 09:47 676 查看

Array对象

初始化Array的三种方式

var cars = ["Audi","BMW","Volvo"];

var cars2 = new Array();
cars2[0] = "Audi2";
cars2[1] = "BMW2";
cars2[2] = "Volvo2";

//字符串分割为数组
var str = "how are you today";
var arr = str.split(" ");

Array的属性/方法

var myArr=new Array("01","02","03");
var myArr2=["001","002","003"];
var myArr3=new Array();
myArr3[0]="100";
myArr3[1]="200";
myArr3[2]="300";
var myArr4=[11,22,33];
var nums=[10,9,3,4,1,2,5];

document.write(myArr.concat("04","05")); //连接数组,追加元素
document.write("<br>");
document.write(myArr2.concat(myArr3));//连接数组,使用另一个数组对象
document.write("<br>");
document.write("myArr2 concat myArr3 and slice(0,4):"+myArr2.concat(myArr3).slice(0,4));//不修改原数组。返回下标0~3的新数组,不含下标4
document.write("<br>");
document.write(" length:"+myArr2.concat(myArr3).slice(0,4).length);//数组长度属性
document.write("<br>");
document.write("myArr4 to string:"+myArr4.toString());//返回以逗号分隔的字符串
document.write("<br>");
document.write("myArr4 reverse:"+myArr4.reverse());//排序颠倒,修改原数组
document.write("<br>");
var tmpp=myArr4.join("~");document.write("myArr4 join:"+tmpp);//返回以指定分隔符分割的字符串
sortfun=function(a,b)
{
return b-a;// descent order
}
document.write("<br>");
document.write("nums sort:"+nums.sort()+"<br>");//以默认方式排序(字母升序),修改原数组,无副本返回

String对象

String的属性/方法

var test_var="i love you!";

document.write("length: "+test_var.length);	//字符串长度属性
document.write("<br>");
document.write("charAt(3): "+test_var.charAt(3));	//返回字符,从下标0开始
document.write("<br>");
document.write("charCodeAt(3): "+test_var.charCodeAt(3));	//返回字符的编码
document.write("<br>");
document.write("indexof('ove'): "+test_var.indexOf("ove"));	//返回子字符串起始下标
document.write("<br>");
document.write("substring(2,6): "+test_var.substring(2,6));	//从下标2开始到下标6之间的字符
document.write("<br>");
document.write("substr(2,4): "+test_var.substr(2,4));	//从下标2开始的4个字符
document.write("<br>");
document.write("toUpperCase: "+test_var.toUpperCase());	//大写

Math对象

Math 对象没有构造函数 Math(),像 Math.sin() 这样的函数只是函数,不是某个对象的方法。
无需创建它,通过把 Math 作为对象使用就可以调用其所有属性和方法。

String的属性/方法

var dig=3.1415926;

document.write("<br>");
document.write("<br>");
document.write("abs: "+Math.abs(dig));	//绝对值
document.write("<br>");
document.write("round: "+Math.round(dig));	//四舍五入
document.write("<br>");
document.write("sqrt: "+Math.sqrt(dig));	//平方根
document.write("<br>");
document.write("ceil: "+Math.ceil(dig));	//小于dig的最大正整数
document.write("<br>");
document.write("floor: "+Math.floor(dig));	//大于dig的最小正整数
document.write("<br>");
document.write("sin: "+Math.sin(dig));	//正弦
document.write("<br>");
document.write("asin: "+Math.asin(dig));	//反正弦
document.write("<br>");
document.write("exp 2: "+Math.exp(2));	//e的2次幂
document.write("<br>");
document.write("pow 2,3: "+Math.pow(2,3));	//2的3次幂
document.write("<br>");
document.write("log 10: "+Math.log(10));	//以e为底的对数
document.write("<br>");
document.write("max 2 5: "+Math.max(2,5));	//最大值
document.write("<br>");
document.write("min 2 5: "+Math.min(2,5));	//最小值
document.write("<br>");
document.write("random: "+Math.random());	//0~1间随机数

自定义对象

自定义对象的四种方式
//method 1
student = new Object();
student.name = "Tom";
student.age = "19";

student.study=function(){
alert("studying");
};

student.eat=function(){
alert("eating");
};

//method 2
var student = {}
student.name = "Tom";
student.age = "19";
student.study=function(){
alert("studying1");
}
student.eat=function(){
alert("eating1");
}

//method 3
var student = {
name:"Tom",
age:"19",
study:function(){
alert("studying2");
},
eat:function(){
alert("eating2");
}
};

//call obj's function with the above 3 methods
student.study();
student.eat();

//method 4
function student(name,age){
this.name=name;
this.age=age;
this.study=function(){
alert("studying3");
};
this.eat=function(){
alert("eating3");
};
}

var std1 = new student("oh","no");
std1.study();
std1.eat();
var x=std1.name;
var y=std1.age;
document.write(x+"!!!"+"<br>"+y);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐