JavaScript基础----40JS面向对象-JS面向对象(2)

来源:互联网 发布:sql中怎么表示至少 编辑:程序博客网 时间:2024/05/16 14:52
<!DOCTYPE html><!--JS面向对象-JS面向对象(2)--><!--上一节中讲的面向对象叫为复杂,本节讲一个简单形式--><html><head lang="en">    <meta charset="UTF-8">    <title></title></head><body><!--创建类,实现继承--><script>//创建一个类function People(){var mythis={};mythis.sayHello=function(){alert("hello")}return mythis;}//    继承Peoplefunction Teacher(){var mythis=People();return mythis;}var tea=Teacher();tea.sayHello();</script><!--创建类,实现继承,并重写方法--><script>//创建一个类function People(){var mythis={};mythis.sayHello=function(){alert("people-hello")}return mythis;}//    继承Peoplefunction Teacher(){var mythis=People();var mySuperSay=mythis.sayHello();mythis.sayHello=function(){//            强行调用父类的被重写的方法//            mySuperSay.call(this);alert("teacher-hello")}return mythis;}var tea=Teacher();//    调用的是重写后的方法tea.sayHello();</script><!--创建类,实现继承,并重写方法,传参--><script>//创建一个类function People(name){var mythis={};mythis.name=name;mythis.sayHello=function(){alert("people-hello"+mythis.name)}return mythis;}//    继承Peoplefunction Teacher(name){var mythis=People(name);var mySuperSay=mythis.sayHello();mythis.sayHello=function(){//            强行调用父类的被重写的方法//            mySuperSay.call(this);alert("teacher-hello"+mythis.name)}return mythis;}var tea=Teacher("zhh");//    调用的是重写后的方法tea.sayHello();</script><!--创建类,实现继承,并重写方法,传参,封装--><!--(function(){        分装的内容    }()    );--><script>    (        function () {            //创建一个类//            创建一个内部的属性            var zhh = "-wo shi zhaihaohao"            function People(name) {                var mythis = {};                mythis.name = name;                mythis.sayHello = function () {                    alert("people-hello" + mythis.name + zhh)                }                return mythis;            }//            对外暴露接口            window.People = People;        }()    );    //    继承People    function Teacher(name) {        var mythis = People(name);        var mySuperSay = mythis.sayHello();        mythis.sayHello = function () {//            强行调用父类的被重写的方法//            mySuperSay.call(this);            alert("teacher-hello" + mythis.name)        }        return mythis;    }    var tea = Teacher("zhh");    //    调用的是重写后的方法    tea.sayHello();</script></body></html>
0 0