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

js中的构造函数和this关键字

2008-12-12 15:05 190 查看
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>无标题页</title>

</head>

<body>

<script language="javascript" type="text/javascript">

function Person(){

this.age="18";

this.name="Joho";

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>无标题页</title>

</head>

<body>

<script language="javascript" type="text/javascript">

function Person(){

this.age="18";

this.name="Joho";

this.sex="Girl";

this.speak=function(){

document.write("Hi,I'm speaking..."+"<br>");

}

this.run=run;

}

function run(){

document.write("Hi, I'm running....");

}

var person=new Person();

person.height=180;//添加属性

document.write("name="+person.name+"<br>")

document.write("age="+person.age+"<br>");

//使用[] 访问对象的属性和方法

document.write("age="+person["age"]+"<br>");

var person2=new Person();//

document.write("name="+person.height+"<br>")//person2中并没有添加height属性,所以会出现错误提示!

person.speak();

person.run();

//删除属性和方法

delete person.age; //删除了该属性后,输出:name=undefined delete person.speak;

person.height=null; //对象的废除

//无用的对象实例会耗用大量的内存空间.废除不再使用的对象来释放内存空间是一个良好的编程习惯.

在javascript中,当一个对象没有变量引用时,该对象就被废除了,在适当时机于用存储单元收集程序会将其

作为垃圾将其从内存中清除,所以要废除对象可以将该对象的所有引用设置为null

window.setTimeout("person.run()",2000);//设置过2秒钟执行!

</script>

</body>

</html>

this.sex="Girl";

this.speak=function(){

document.write("Hi,I'm speaking..."+"<br>");

}

this.run=run;

}

function run(){

document.write("Hi, I'm running....");

}

var person=new Person();

person.height=180;//添加属性

document.write("name="+person.name+"<br>")

document.write("age="+person.age+"<br>");

//使用[] 访问对象的属性和方法

document.write("age="+person["age"]+"<br>");

var person2=new Person();//

document.write("name="+person.height+"<br>")//person2中并没有添加height属性,所以会出现错误提示!

person.speak();

person.run();

//删除属性和方法

delete person.age; //删除了该属性后,输出:name=undefined delete person.speak;

person.height=null; //对象的废除

//无用的对象实例会耗用大量的内存空间.废除不再使用的对象来释放内存空间是一个良好的编程习惯.

在javascript中,当一个对象没有变量引用时,该对象就被废除了,在适当时机于用存储单元收集程序会将其

作为垃圾将其从内存中清除,所以要废除对象可以将该对象的所有引用设置为null

window.setTimeout("person.run()",2000);//设置过2秒钟执行!

</script>

</body>

</html>

with的使用

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

2

3<html xmlns="http://www.w3.org/1999/xhtml">

4<head>

5 <title>用语对象的语句</title>

6</head>

7<body>

8</script>

27</body>

28</html>

29

30
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: