<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
定义并创建对象实例
<br/>
<script>
people = new Object();
people.name="Tom";
people.age="20";
document.write("name:"+people.name+",age:"+people.age);
document.write("<br/>");
person= {name:"jerry",age:"21"};
document.write("name:"+person.name+",age:"+person.age);
</script>
<br/>
<br/>
使用函数来定义对象,然后创建新的对象实例
<br/>
<script>
function pet(name,age){
this.name = name;
this.age = age;
}
someone = new pet("Tade",15);
document.write("name:"+someone.name+",age:"+someone.age);
</script>
<br/>
<br/>
String字符串对象
<br/>
<script>
var str = "Hello World";
var str1 = "Hello world hello html hello js"
document.write("字符创长度为:"+str.length)
document.write("<br/>");
document.write("在字符串中查找字符串,如果存在就返回该字符串开始的位置,不存在就返回-1。其中world的位置为:"+str.indexOf("World"));
document.write("<br/>");
document.write("匹配字符串,如果匹配就返回该字符串内容,不匹配就返回-1。其中hello是否匹配:"+str.match("Hello"));
document.write("<br/>");
document.write("替换字符串,将上述字符串中的World替换为jikexueyuan,则有:"+str.replace("World","jikexueyuan"))
document.write("<br/>");
document.write("将小写全部转换为大写:"+str.toUpperCase());
document.write("<br/>");
document.write("将大写全部转换为小写:"+str.toLowerCase());
var s = str1.split(" ");
document.write("将上述字符串转换为数组,以空格为分隔,打印出数组中第4个内容:"+s[4]);
</script>
</body>
</html>
String字符串对象
最新推荐文章于 2024-07-06 20:49:37 发布