一、什么是对象
1.什么是对象:JavaScript中的所有事物都是对象:字符串、数值、数组、函数...
每个对象带有属性和方法
JavaScript允许自定义对象
2.自定义对象:
定义并创建对象实例
使用函数来定义对象,然后创建新的对象实例
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<!DOCTYPE html>
<html>
<head>
<meta charset=
"UTF-8"
>
<title></title>
</head>
<body>
<!--创建对象-->
<script>
// people = new Object();
// people.name = "yeleven";
// people.age = "22";
// document.write("name:"+people.name+",age:"+people.age);
// people = {name:"yeleven",age:"22"};
// document.write("name:"+people.name+",age:"+people.age);
function
people(name,age){
this
.name = name;
this
.age = age;
}
son =
new
people(
"yeleven"
,22);
document.write(
"name:"
+son.name+
",age"
+son.age);
</script>
</body>
</html>
|
二、String字符串对象
1、String对象
String对象用于处理已有的字符串
字符串可以使用单引号或双引号
2、在字符串中查找字符串:indexOf()
3、内容匹配:match()
4、替换内容:replace()
5、字符串大小写转换:toUpperCase()/toLowerCase()
6、字符串转为数组:strong>split()
7、字符串属性和方法:
属性:length、prototype、constructor
方法:charAt()、charCodeAt()、
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<!DOCTYPE html>
<html>
<head>
<meta charset=
"UTF-8"
>
<title></title>
</head>
<body>
<!--创建对象-->
<script>
var
str =
"Hello World"
;
var
str1 =
"hello,yeleven1,yeleven2"
;
// document.write("字符串长度为:"+str.length);
// document.write(str.indexOf("World"));
// document.write(str.match("World"));
// document.write(str.replace("World","Yeleven"));
// document.write(str.toUpperCase());
// document.write(str.toLowerCase());
var
str = str1.split(
","
);
document.write(str[1]);
</script>
</body>
</html>
|
本文转自yeleven 51CTO博客,原文链接:http://blog.51cto.com/11317783/1793785