1. 程式人生 > >javascrpt new建構函式簡要說明

javascrpt new建構函式簡要說明

    /*
        new + 建構函式  (函式可以自定義,建構函式首字母必須大寫) === 工廠模式
        比如function Student()
        就可以用new Student() 建立物件  物件的屬性xx就可以通過Student().xx 呼叫
        或者給物件一個例項charlie = new Student()     charlie.xx 呼叫

    */
    function Student(Englishname,sno,sage) {
        this.ename = Englishname;
        this.sno = sno;
        this.sage = sage;
        message = {
            "name":this.ename,
            "no":this.sno,
            "age":this.sage
        };
        return message
    }
    var 大超 = new Student('charlie','2017105110207',19);
    var 雪瑞 = new Student('icey','171405236',20);
    alert(大超.name)