JavaScript对象
1.
JavaScript对象是拥有属性和方法的数据;(对象是属性和方法的容器)
对象定义:
对象也是一个变量,不过对象可以包含多个值
键值对通常写法为 name : value (键与值以冒号分割)
键值对在 JavaScript对象中通常称为对象属性(对象是键值对的容器)
var y = {size : "xl"};
var add = {
name : "张三",
age : 28,
weight : "70kg",
height : "187cm"
}
document.write(add.name + add.age + "岁" + add.weight + add.height);
访问对象属性:
js中的点号用于取对象属性、方法:
- 1、对象名.属性名;
- 2、对象名[“属性名”];
访问对象方法:
对象的方法定义了一个函数并将这个函数作为对象的属性存储。
对象方法通过添加 () 调用 (作为一个函数)。
this 关键字
this 指向函数执行时的当前对象,也就是num这个对象。
var num = {
a : 3,
b : 4,
c : function(){
return this.a * this.b
}
}
document.write(num.c());
2.
javascript对象常用属性
prototype 属性允许您向对象添加属性和方法
语法:object.prototype.name = value
其中object为对象名称,name为属性名称,value为属性值。
function myfun(a,b){
this.firstName = a;
this.lastName = b;
}
var x = new myfun("张","三丰")//使用new关键字创建对象
myfun.prototype.ages = "80岁";
document.write(x.firstName + x.lastName + x.ages + "<br>");
for/in 语句用于循环对象属性( for…in 循环中的代码块将针对每个属性执行一次)
语法:
for (var in object) {
执行的代码块
}
var add={
name:"张三",
age:"28岁",
height:"187cm"
}
var x; //x表示add对象中的数据
var txt = ""; //txt的作用是用于储存读取的数据
for(x in add){
txt = txt + add[x] + ";" //每一次读取的属性值就储存在txt中
}
document.write(txt);
String属性及方法:
计算字符串长度:
var txt = "hello html";
document.write(txt.length);//lenght属性可以返回字符长度;
charAt():返回一个字符值,是指定索引位置的值
var txt = "hello html";
document.write(txt.charAt(2));//该方法从0开始计数,所以输出l
match():函数用来查找字符串中特定的字符
var txt = "hello html";
document.write(txt.match("html"));//如果没有找到,那么返回null
indexOf():指出子字符串的开始位置,除去第一个必选参数,还有一个参数fromindex,这是可选参数,意思是从该位置开始检索
var str = "how do you do";
document.write(str.indexOf("do"));//查找字符(返回的是整数值),位置从0开始
lastIndexOf() :指出最后一个子字符串的开始位置,如果要检索的字符串值没有出现,则该方法返回 -1
replace(要被替换的字符串,需要被替换成字符串):字符串替换函数
var x = stringObject.replace("html","html中文网");//注意两个参数的位置
document.write(x);
concat():可以连接两个或者多个字符串;
var y = "中文网呀!";
document.write(stringObject.concat(y));//concat括号中是我们需要拼接的一个字符串
substring(start,stop):返回一个子字符串,stop可选,若忽略则意味着从start到结束
document.write(stringObject.substring(1, 7));
substr(start,length):返回一个子字符串, start必需,要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。length可省略
document.write(stringObject.substr(1, 3));
slice(start, end) :可提取字符串的某个部分,并以新的字符串返回被提取的部分。start要抽取的片断的起始下标。如果是负数,则该参数规定的是从字符串的尾部开始算起的位置。也就是说,-1 指字符串的最后一个字符,-2 指倒数第二个字符,以此类推。end紧接着要抽取的片段的结尾的下标。若未指定此参数,则要提取的子串包括 start 到原字符串结尾的字符串。如果该参数是负数,那么它规定的是从字符串的尾部开始算起的位置。效果同Array.slice()
<script type="text/javascript">
var str="Hello happy world!"
document.write(str.slice(6))
</script>
输出:
happy world!
数组Array
优点:方便存取,并且减少内存占用,
作用:使用单独变量名来储存一系列的值
下标(房间号)从0开始
创建数组:
var a = new Array()
a[0] = 1;
a[1] = 2;
……
a[n] = n + 1;
var a = Array(1, 2, 3, 4 …… n);
var a = ['php', 'html', 'js'];
访问数组 通过指定的数组名,以及索引号码(下标)
document.write(a[0]);
二维数组:
var b = new Array(2);
b[0] = new Array(1, 2, 3);
b[1] = new Array(6, 7, 8);
document.write(b[0] + "<br>");
document.write(b[0][2]);
循环输出数组中元素的方法:for in 循环
var week = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
var x;
for(x in week)
{
document.write(week[x]);
}
数组操作函数(方法)
数组合并函数:concat(item1,item2 ……)
var a, b, c;
a = [1, 2, 3, 4, 5];
b = Array(6, 7, 8, 9, 10);
c = ["php", "html", "js", "jq"];
document.write(a.concat(b) + "<br>");
document.write(a.concat(b,c));
将数组元素变成字符串,并且以指定分割符号分割 join() :
document.write(arrayObject.join(">"));//在括号中没有符号时,默认是逗号,当然你也可以使用其他的符号,用双引号引起来
删除元素并向数组添加新元素 splice(起始位置,删除个数,新元素)
document.write(arrayObject.splice(1,1,"web前端") + "<br>");//他的返回是我们删除掉的元素
document.write(arrayObject);
输出:
html
php,web前端,js,jq
从数组中返回选定的元素 slice(起始位置,终止)
document.write(arrayObject.slice(0,2));
push()向数组末尾添加一个或者多个元素,并返回一个新数组的长度
document.write(arrayObject.push("push"));
document.write(arrayObject);
pop() :方法将删除 arrayObject 的最后一个元素,把数组长度减 1,并且返回它删除的元素的值。如果数组已经为空,则 pop() 不改变数组,并返回 undefined 值。
document.write(arrayObject .pop());
document.write(arrayObject);
Date对象方法:
日期对象是用以处理日期和时间的
获取当前时间 :
var myday = new Date();
document.write(myday);
获取年份 getFullYear():
document.write(myday.getFullYear() + "年");
setFullYear(year,month,day) :方法用于设置年份,月份和日期可选,月份从0 ~ 11,日期从1 ~ 31
var d = new Date();
d.setFullYear(2018, 10, 3);
document.write(d);
获取月份 getMonth() 0代表的是1月,11代表的是12月
document.write(myday.getMonth() + "月");
var month = new Array(12);
month[0] = "一月";
month[1] = "二月";
month[2] = "三月";
month[3] = "四月";
month[4] = "五月";
month[5] = "六月";
month[6] = "七月";
month[7] = "八月";
month[8] = "九月";
month[9] = "十月";
month[10] = "十一月";
month[11] = "十二月";
document.write("今天是" + month[myday.getMonth()]);
获取星期 getDay():0代表的是周日,6代表是周六(返回值是0~6之间的一个整数)
document.write(myday.getDay());
获取日期getDate()返回值是0~31之间的一个整数
document.write(myday.getDate());
获取小时 getHours()(返回值是0~23之间的一个整数)
获取分钟 getMinutes()(返回值是0~59之间的一个整数)
Math 对象的作用:执行普通的算数任务;
Math 对象提供多种算数值类型和函数。无需在使用这个对象之前对它进行定义
round() 方法可把一个数字舍入为最接近的整数;
语法:Math.round(x) x必须是数字
var a = Math.round(2.4);
document.write(a);
random() 方法可返回介于 0(包含)~ 1(不包含)之间的一个随机数;
document.write(Math.random());
floor()方法返回小于等于x的最大整数,(如果传递的参数是一个整数,该值不变):
语法:Math.floor(x) x任意数值或表达式
var b = Math.floor(3.8);
document.write(b + "<br>");
document.write("【案例】取得介于 1 到 10 之间的一个随机数" + "<br>");
var c = Math.floor((Math.random() * 100) + 1);
document.write(c);
max() 方法可返回两个指定的数中带有较大的值的那个数:
语法:Math.max(n1,n2,n3,…,nx)
var d = Math.max(2, 150, 30, 50, 70);
document.write(d);
min() 方法可返回指定的数字中带有最小值的数字:
语法:Math.max(n1,n2,n3,…,nx)
var e = Math.min(2, 150, 30, 50, 70);
document.write(e);