zoukankan      html  css  js  c++  java
  • 对象继承方式

    <!doctype html>
    <html lang="en">
     <head>
      <meta charset="UTF-8">
    
      <title>对象继承</title>
     </head>
     <body>
      <script>
      /*
             function Person(name, age){
                this.name = name;
                this.age = age;
                this.say = function(){
                    alert("我的名字是" + this.name);
                }
            }
    
            function Student(name,id){
                this.temp = Person; //临时属性方式,对象冒充
                this.temp(name);
                //delete this.temp;
                this.id = id;
                this.showId = function(){
                    alert('我是学生,我的学号是:' + this.id);
                }
            }
    
            var stu = new Student('zhangsan','001');
            stu.say();
            stu.showId();
        */
        //*****************************************************
            /*function Person(name, age){
                this.name = name;
                this.age = age;
                this.say = function(){
                    alert("我的名字是" + this.name);
                }
            }
    
            function Student(name,id){
                Person.call(this,name);//call方式实现对象冒充
                //Person.apply(this,new Array(name)); //apply方式实现冒充
                this.id = id;
                this.showId = function(){
                    alert('我是学生,我的学号是:' + this.id);
                }
            }
    
            var stu = new Student('zhangsan','001');
            stu.say();
            stu.showId();
            */
            /*
            在OO概念中,new实例化后,对象就在堆内存中形成了自己的空间,值得注意的是,这个代码段。而成员方法就是存在这个代码段的,并且方法是共用的。问题就在这里,通过对象冒充方式继承时,所有的成员方法都是指向this的,也就是说new之后,每个实例将都会拥有这个成员方法,并不是共用的,这就造成了大量的内存浪费。并且通过对象冒充的方式,无法继承通过prototype方式定义的变量和方法,如以下代码将会出错:*/
            //***************************************
            /*function Person(name){
                this.name = name;
                this.say = function(){
                    alert("我的名字是" + this.name);
                }
            }
            Person.prototype.age = 20;
            Person.prototype.sayAge = function(){
                    alert('我的年龄是:' + this.age);
                }
    
            function Student(name,id){
                //Person.call(this, age);
                Person.apply(this, new Array(age));
                this.id = id;
                
                this.showId = function(){
                    alert('我是学生,我的学号是:' + this.id);
                }
            }
    
            var stu = new Student('zhangsan','001');
            stu.sayAge(); //报错age 未定义
            //stu.showId();
            */
            //*********************************************************
            //原型继承方式:此方法实现有缺陷,就是实例化子类时不能把参数传给父类,function Person()没有参数。
            /*
                function Person(){
                this.name = 'wangwu';
                
            }
            Person.prototype.say = function(){
                alert('my name is :' + this.name);
            }
    
            function Student(id){
                this.id = id;
                this.showId = function(){
                    alert('I am a student,my NO is:' + this.id);
                }
            }
            Student.prototype = new Person();
    
            var stu = new Student('001');
            stu.say();//zhangsan
            stu.showId();
            alert(stu.hasOwnProperty('id'));
            */
            //*******************************************************
            //好的继承方式是,成员变量用对象冒充,成员方法用原型继承
            function Person(name){
                this.name = name;
                
            }
            Person.prototype.say = function(){
                alert('my name is :' + this.name);
            }
    
            function Student(name,id){
                Person.call(this, name);
                this.id = id;
                this.showId = function(){
                    alert('I am a student,my NO is:' + this.id);
                }
            }
            Student.prototype = new Person('wangwu');
    
            var stu = new Student('zhangsan','001');
            stu.say();//zhangsan
            stu.showId();
            alert(stu.hasOwnProperty('id'));
            
      </script>
     </body>
    </html>
  • 相关阅读:
    BZOJ3282 Tree
    [NOI2004] 郁闷的出纳员
    [HNOI2004]宠物收养所
    [HNOI2002] 营业额统计
    图论 简单学习笔记
    POJ3321 Apple tree
    [国家集训队] 聪聪可可
    POJ2976 Dropping tests
    SCOI2005 最大子矩阵
    codeforces|CF13C Sequence
  • 原文地址:https://www.cnblogs.com/double405/p/4593895.html
Copyright © 2011-2022 走看看