在Javascript定义对象

在Javascript定义对象

var xiaoxing = 
{       
   grade:1,
   name : "xiaoxing",
   age:27,
   sex:"男",
      
   speak:function(words)
   {
      alert(this.name+"说:"+words+"!");
   },

   improve:function()
   {
       this.grade++;
   }
}
这样,我就变成了一个对象了,你可以让我跟你说一句话
xiaoxing.speak("欢迎来到cnblogs的supersun的天空");
while(i have friends)
{
   xiaoxing.improve();
}
还不够面向对象,最好的做法是用一个函数来定义对象 ,在用他的prototype原型对象的方式来定义其属性,事件等,方便继承扩展。
<script language="javascript" type="text/javascript">
//创建原型对象,定义属性、方法、及对象事件等。 
Student.prototype=
{ 
    _name:null, 
    _age:null, 
    _sex:null, 
    ShowName:function()
    { 
        alert("Name:"+ this._name +"/n" + "Age:" + this._age + "/n" + "Sex:"+ this._sex); 
    } 
} 
//专门用一个函数来初始化对象。 
function Student(name,age,sex)
{ 
    this._name=name; 
    this._age=age; 
    this._sex=sex; 
} 

var student=new Student("Young",25,"男"); //实例化 
student.ShowName(); //调用对象方法
</script>
<script language="javascript" type="text/javascript"> //创建原型对象,定义属性、方法、及对象事件等。 Student.prototype= { _name:null, _age:null, _sex:null, ShowName:function() { alert("Name:"+ this._name +"/n" + "Age:" + this._age + "/n" + "Sex:"+ this._sex); } } //专门用一个函数来初始化对象。 function Student(name,age,sex) { this._name=name; this._age=age; this._sex=sex; } var student=new Student("Young",25,"男"); //实例化 student.ShowName(); //调用对象方法 </script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> New Document </TITLE> 
<META NAME="Generator" CONTENT="EditPlus"> 
<META NAME="Author" CONTENT=""> 
<META NAME="Keywords" CONTENT=""> 
<META NAME="Description" CONTENT=""> 
</HEAD> 

<BODY> 
<script language="javascript" type="text/javascript">
function Student(name,age,sex) 
{ 
    this._name=name; 
    this._age=age; 
    this._sex=sex; 
    this.ShowName=function() 
    { 
        alert("Name:"+ this._name +"/n" + "Age:" + this._age + "/n" + "Sex:"+ this._sex); 
    }; 
} 

var student=new Student("Young",25,"男"); //实例化 
student.ShowName(); //调用对象方法
</script> 
</BODY> 
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <script language="javascript" type="text/javascript"> function Student(name,age,sex) { this._name=name; this._age=age; this._sex=sex; this.ShowName=function() { alert("Name:"+ this._name +"/n" + "Age:" + this._age + "/n" + "Sex:"+ this._sex); }; } var student=new Student("Young",25,"男"); //实例化 student.ShowName(); //调用对象方法 </script> </BODY> </HTML>
<script type="text/javascript">
var person = new Function();//或var person = function(){}都可以 
person.prototype={ 
    grade:0, 
    age:0, 
    sex:null, 
    name:null, 
    speak:function(words) 
    { 
        alert(this.name+"说:"+words+"!"); 
    }, 
    init:function(_grade,_age,_sex,_name) 
    { 
        this.grade = _grade;this.age=_age;this.sex=_sex;this.name=_name; 
    } 
} 
var xiaoxing = new person(); 
xiaoxing.init("10","27","男","xiaoxing"); 
xiaoxing.speak("hello everybody");
</script>
<script type="text/javascript"> var person = new Function();//或var person = function(){}都可以 person.prototype={ grade:0, age:0, sex:null, name:null, speak:function(words) { alert(this.name+"说:"+words+"!"); }, init:function(_grade,_age,_sex,_name) { this.grade = _grade;this.age=_age;this.sex=_sex;this.name=_name; } } var xiaoxing = new person(); xiaoxing.init("10","27","男","xiaoxing"); xiaoxing.speak("hello everybody"); </script>
这里采用init来初始化定义了一个对象,当然我们还可以在function(){}这样写
var person = function(_grade,_age,_sex,_name){this.grade = _grade;this.age=_age;this.sex=_sex;this.name=_name;}
这样我们就可以写成如下代码
var xiaoxing = new person("10","27","男","xiaoxing"); 
xiaoxing.speak("hello everybody");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值