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

来源:互联网 发布:石油 知乎 编辑:程序博客网 时间:2024/05/16 06:45

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的新数组,不含下标4document.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 1student = new Object();student.name = "Tom";student.age = "19";student.study=function(){alert("studying");};student.eat=function(){alert("eating");};//method 2var student = {}student.name = "Tom";student.age = "19";student.study=function(){alert("studying1");}student.eat=function(){alert("eating1");}//method 3var student = {name:"Tom",age:"19",study:function(){alert("studying2");},eat:function(){alert("eating2");}};//call obj's function with the above 3 methodsstudent.study();student.eat();//method 4function 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);


阅读全文
0 0
原创粉丝点击