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
<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
相关文章推荐
- 如何让你快速轻松理解js的函数和构造函数的区别
- JS——构造函数的三种方法
- 详细讲解js中静态对象和构造函数的区别
- js面向对象编程(二)构造函数的继承
- 知识点总结(构造函数,构造代码块儿,this关键字等对比)
- js中的this关键字详解
- Js中的this关键字(吉木自学)
- js工厂模式(利用构造函数)
- 由一道js题探讨构造函数、prototype和__proto__之间的关系
- js基础知识温习:构造函数与原型
- js对象、构造函数、命名空间、方法、属性
- 构造函数,this关键字
- JAVA 封装、构造函数、this关键字 day7
- js--3.对象-9.构造函数
- [js高手之路]构造函数的基本特性与优缺点
- js Date类型的构造函数及其parse的用法
- JS的构造函数
- Java学习·this关键字在构造函数间调用
- JavaScript高级程序设计(第2版) 学习笔记:(六)js基于构造函数与原型的继承
- JS面向对象基础讲解(工厂模式、构造函数模式、原型模式、混合模式、动态原型模式)