Es5中的類和靜態方法 繼承

 

Es5中的類和靜態方法 繼承(原型鏈繼承、對象冒充繼承、原型鏈+對象冒充組合繼承)函數

 

// es5裏面的類  

//1.最簡單的類
// function Person(){
//     this.name='張三';
//     this.age=20;
// }
// var p=new Person();
// alert(p.name);


//二、構造函數和原型鏈裏面增長方法
// function Person(){
//     this.name='張三';  /*屬性*/
//     this.age=20;
//     this.run=function(){
//         alert(this.name+'在運動');
//     }
// }
// //原型鏈上面的屬性會被多個實例共享   構造函數不會
// Person.prototype.sex="男";
// Person.prototype.work=function(){
//     alert(this.name+'在工做');
// }
// var p=new Person();
// // alert(p.name);
// // p.run();
// p.work();


//3類裏面的靜態方法
// function Person(){
//     this.name='張三';  /*屬性*/
//     this.age=20;
//     this.run=function(){  /*實例方法*/
//         alert(this.name+'在運動');
//     }
// }

// Person.getInfo=function(){
//     alert('我是靜態方法');
// }
// //原型鏈上面的屬性會被多個實例共享   構造函數不會
// Person.prototype.sex="男";
// Person.prototype.work=function(){
//     alert(this.name+'在工做');
// }
// var p=new Person();    
// p.work();
// //調用靜態方法
// Person.getInfo();



// 四、es5裏面的繼承   對象冒充實現繼承
//    function Person(){
//         this.name='張三';  /*屬性*/
//         this.age=20;
//         this.run=function(){  /*實例方法*/
//             alert(this.name+'在運動');
//         }
//     }      
//     Person.prototype.sex="男";
//     Person.prototype.work=function(){
//          alert(this.name+'在工做');
//     }
//     //Web類 繼承Person類   原型鏈+對象冒充的組合繼承模式
//     function Web(){
//         Person.call(this);    /*對象冒充實現繼承*/
//     }
//     var w=new Web();
//    // w.run();  //對象冒充能夠繼承構造函數裏面的屬性和方法
//     w.work();  //對象冒充能夠繼承構造函數裏面的屬性和方法   可是無法繼承原型鏈上面的屬性和方法



// 五、es5裏面的繼承   原型鏈實現繼承
//    function Person(){
//         this.name='張三';  /*屬性*/
//         this.age=20;
//         this.run=function(){  /*實例方法*/
//             alert(this.name+'在運動');
//         }
//     }      
//     Person.prototype.sex="男";
//     Person.prototype.work=function(){
//          alert(this.name+'在工做');
//     }
//     //Web類 繼承Person類   原型鏈+對象冒充的組合繼承模式
//     function Web(){
//     }
//    Web.prototype=new Person();   //原型鏈實現繼承
//    var w=new Web();
//     //原型鏈實現繼承:能夠繼承構造函數裏面的屬性和方法 也能夠繼承原型鏈上面的屬性和方法
//     //w.run();
//     w.work();



// 六、 原型鏈實現繼承的 問題?
//    function Person(name,age){
//         this.name=name;  /*屬性*/
//         this.age=age;
//         this.run=function(){  /*實例方法*/
//             alert(this.name+'在運動');
//         }
//     }      
//     Person.prototype.sex="男";
//     Person.prototype.work=function(){
//          alert(this.name+'在工做');
//     }
//    var p=new Person('李四',20);
//    p.run();



// function Person(name,age){
//         this.name=name;  /*屬性*/
//         this.age=age;
//         this.run=function(){  /*實例方法*/
//             alert(this.name+'在運動');
//         }
// }      
// Person.prototype.sex="男";
// Person.prototype.work=function(){
//         alert(this.name+'在工做');
// }


// function Web(name,age){
// }

// Web.prototype=new Person();
// var w=new Web('趙四',20);   //實例化子類的時候無法給父類傳參
// w.run();
// // var w1=new Web('王五',22);



//7.原型鏈+對象冒充的組合繼承模式
//   function Person(name,age){
//             this.name=name;  /*屬性*/
//             this.age=age;
//             this.run=function(){  /*實例方法*/
//                 alert(this.name+'在運動');
//             }
//     }      
//     Person.prototype.sex="男";
//     Person.prototype.work=function(){
//             alert(this.name+'在工做');
//     }

//     function Web(name,age){
//         Person.call(this,name,age);   //對象冒充繼承   實例化子類能夠給父類傳參
//     }
//     Web.prototype=new Person();
//     var w=new Web('趙四',20);   //實例化子類的時候無法給父類傳參
//     // w.run();
//     w.work();
//     // var w1=new Web('王五',22);



//八、原型鏈+對象冒充繼承的另外一種方式
function Person(name, age) {
    this.name = name;  /*屬性*/
    this.age = age;
    this.run = function () {  /*實例方法*/
        alert(this.name + '在運動');
    }
}
Person.prototype.sex = "男";
Person.prototype.work = function () {
    alert(this.name + '在工做');
}
function Web(name, age) {
    Person.call(this, name, age);   //對象冒充繼承  能夠繼承構造函數裏面的屬性和方法、實例化子類能夠給父類傳參
}
Web.prototype = Person.prototype;
var w = new Web('趙四', 20);   //實例化子類的時候無法給父類傳參
w.run();
// w.work();
// var w1=new Web('王五',22);
相關文章
相關標籤/搜索