javascript基于原型的继承

 <script type="text/javascript">
        function Person(name, age) {
            this.name = name;
            this.age = age;
            this.getName = function () {
                return this.name;
            }
            this.setName = function (name) {
                this.name = name;
            }
        }
        function Teacher() {
            this.sub = 'PC';
            this.print = function () {
                alert(this.age + '  ' + this.sub + '  ' + this.getName());
            }
        }
        Teacher.prototype = new Person("www", 12);//Teacher 继承了Person的一切!!
        var t = new Teacher();
        //t.setName("wj");
        t.print();

    </script>


你可能感兴趣的:(javascript基于原型的继承)