Learn to use Class in Javascript

<script language="javascript" type="text/javascript">
// <!CDATA[
// use Array
var gsArray =  new Array('a','b','c','d');

function Button1_onclick() {
    var a ="";
//    for(var i=0;i<gsArray.length;i++)
//        a += gsArray[i];
//    document.write(a);

    // create object1
    var person = new Person("George","Male",25);
    person.SetEmail("geforcesong@gmail.com");
    person.Show();
    person.Trim();
   
    // create object2
    var jenny = new Person("Jenny","Female",26);
    jenny.Email = "greenfish_2005@hotmail.com";
    jenny.Show();
   
    var str = new String('aaaaaaaaaa')
    str.AAA();
}

function Person(Name,Sex,Age)
{
    // the general function
    function Show()
    {
        var message = "";
        // this.GetMessage(), 这样的调用方法是必须的,如果不加this的话就代表不是当前对象,会得到错误的结果。
        message = this.GetMessage();
        alert(message);
    }
   
    // the member function which contains a parameter
    function SetEmail(el)
    {
        this.Email = el;
    }
   
    // the function which returns a string
    function GetMessage()
    {
        var message = "";
        message = this.Name + "<br/>" + this.Sex + "<br/>" + this.Age + "<br />" + this.Email;
        return message;
    }
   
    // it is another way of registering your function.
    Person.prototype.Trim = function()
    {
        alert("Trim" + this.Name);
    }
   
    // properties
    this.Name = Name;
    this.Sex = Sex;
    this.Age = Age;
    this.Email = null;
    // member functions
    // if the function will be call through object, it must be registed here as follows,
    this.Show = Show;
    this.SetEmail = SetEmail;
    this.GetMessage = GetMessage;
}

// it can extend javascript class,e.g the code follows extended the class String. It make it has anoter extra funcation AAA.
String.prototype.AAA = function()
{
    alert(this);
}
// ]]>
</script> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值