js对象

来源:互联网 发布:java域对象是什么意思 编辑:程序博客网 时间:2024/06/01 04:00

对象里var的是私有的 this.的是公有的

 

<html>
<script>

function User()
{
 this.name="";
 this.age="";
 this.getName=function()
 {
 return this.name;
 }
 this.getAge=function()
 {
 return this.age;
 }
 this.setName=function(name)
 {
 this.name=name;
 }
 this.setAge=function(age)
 {
 this.age=age;
 }

 this.introduction=function()
 {
 alert("My name is" + this.name+"/n" +"age is" +

this.age);
 }

 

}

var user1=new User();
user1.setName("qqq");
user1.setAge("11");
user1.introduction();
alert("My name is" + user1.name+"/n" +"age is" + user1.age);

</script>
</html>

 

<html>
<script>

var Person={};
Person.name="Tom";
Person["age"]=20;
Person.introduction=function()
{
 alert("My name is "+this.name+",my age is

"+this.age);
};

Person.introduction();//My name is Tom age is 20
for(var field in Person)
{
 alert("field name:" + field + ";value:" +Person

[field]);
delete Person.name;
Person.introduction();
alert(name in Person);//false
}

</script>
</html>

 

<html>
<script>
function hello()
{
 alert("Hello!");
}
hello.name="Tom";//添加属性
alert(hello["name"]);
delete hello.name;//删除属性
alert(hello.name);
//赋值给变量
var hello1=function()
{
 alert(hello1);
};
hello1();
//作为数组元素
function show1(x)
{
 alert(x);
}
var arr=[show1];
arr[0](5);
//作为函数的参数
function callFunc(func)
{
 func();
}
callFunc(function()
{
 alert("Inner function.");
});
function show()
{
 return function(n){alert("number is "+n)};
}
show()(10);
alert(arr[0]);
</script>
</html>