zoukankan      html  css  js  c++  java
  • JavaScript从初见到热恋之深度讨论JavaScript中的面向对象。

    JavaScript中的面向对象。面向对象的三个基本特征:封装、继承、多态。

    1、封装

    js的封装如下

    定义Person类

            function  Person(name,age,sex) {
                this.name=name;
                this.age=age;
                this.sex=sex;
                this.showinfo=function () {
                    alert('name:'+name+"age:"+age+"sex:"+sex)
                }
            }

    这个类三个参数 name,age,sex,分别对应的参数的中文名 姓名,年龄,性别

    初始化的过程

      var person=new Person('马良','31','男');
                person.showinfo();

    以上就是一个简单的封装就实现了。然而我们对于Person类的定义可能实现了一部分,还要增加一些属性和方法。我们尝试使用原型进行拓展prototype

            Person.prototype={eat:function (sth) {
                alert("eat" + sth)
            },
                drink:function (sth) {
                    alert("drink"+sth)
                }};

    我们拓展两个方法 eat和drink。然后我们调用

    person.eat("干的")
    person.drink("湿的")

    对于prototype的拓展我们还可以这样写

     Person.prototype.eat=function (sth) {
                alert("eat" + sth)
            };
    
            Person.prototype.drink=function (sth) {
                alert("drink" + sth)
            };

    Person.prototype=会覆盖Person.prototype.写的所有的方法和属性。

    Person.prototype=需要是对象{},不是[],包括属性和方法。

    关于prototype的更多内容,我们可以参考http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html

    对于方法除了刚才我们在类中定义的方法以外,还有一种方法就是类方法。

    Person.newID=function (id) {
       alert(id)
    }

    调用

    Person.newID(10000)

    这类似于静态方法

    2、继承

    2.1、js原型(prototype)实现继承

    定义Person类

            function  Person(name,age,sex) {
                this.name=name;
                this.age=age;
                this.sex=sex;
                this.showinfo=function () {
                    alert('name:'+name+"age:"+age+"sex:"+sex)
                }
            }
            
            Person.prototype.eat=function (sth) {
                alert("eat" + sth)
            };
    
            Person.prototype.drink=function (sth) {
                alert("drink" + sth)
            };

    现在我们定义Student类,继承于Person类

     function Student() {
    
            }
            Student.prototype=new Person("马良",31,'男');
            Student.prototype.ShowStudentInfo=function () {
                alert("Welcome NewStudent")
            }

    通过prototype来继承Person类,我们来调用

                var stu=new  Student();
                stu.showinfo();
                stu.ShowStudentInfo();

    2.构造函数实现继承

    <SPAN style="FONT-SIZE: 18px"><html> 
    <body> 
    <script type="text/javascript"> 
        function  Parent(name){ 
            this.name=name; 
            this.sayParent=function(){ 
                alert("Parent:"+this.name); 
            } 
        } 
    
        function  Child(name,age){ 
            this.tempMethod=Parent; 
            this.tempMethod(name); 
            this.age=age; 
            this.sayChild=function(){ 
                alert("Child:"+this.name+"age:"+this.age); 
            } 
        } 
    
        var parent=new Parent("江剑臣"); 
        parent.sayParent(); //输出:“Parent:江剑臣” 
        var child=new Child("李鸣",24); //输出:“Child:李鸣 age:24” 
        child.sayChild(); 
    </script> 
    </body> 
    </html></SPAN> 

    3.call , apply实现继承

    <SPAN style="FONT-SIZE: 18px"><html> 
    <body> 
    <script type="text/javascript"> 
        function  Person(name,age,love){ 
            this.name=name; 
            this.age=age; 
            this.love=love; 
            this.say=function say(){ 
                alert("姓名:"+name); 
            } 
        } 
    
        //call方式 
        function student(name,age){ 
            Person.call(this,name,age); 
        } 
    
        //apply方式 
        function teacher(name,love){ 
            Person.apply(this,[name,love]); 
            //Person.apply(this,arguments); //跟上句一样的效果,arguments 
        } 
    
        //call与aplly的异同: 
        //1,第一个参数this都一样,指当前对象 
        //2,第二个参数不一样:call的是一个个的参数列表;apply的是一个数组(arguments也可以) 
    
        var per=new Person("武凤楼",25,"魏荧屏"); //输出:“武凤楼” 
        per.say(); 
        var stu=new student("曹玉",18);//输出:“曹玉” 
        stu.say(); 
        var tea=new teacher("秦杰",16);//输出:“秦杰” 
        tea.say(); 
    
    </script> 
    </body> 
    </html></SPAN>  

    3、多态

    多态就比较简单了,之前我们定义的方法如下

     Person.prototype.eat=function (sth) {
                alert("eat" + sth)
            };

    现在我们进行重写实现多态

     Person.prototype.eat=function () {
            };

     大功告成。希望大家受益。

    4、call和apply的用法(详细介绍)

    js中call和apply都可以实现继承,唯一的一点参数不同,func.call(func1,var1,var2,var3)对应的apply写法为:func.apply(func1,[var1,var2,var3])。

    JS手册中对call的解释:

    <SPAN style="FONT-SIZE: 18px">call 方法 
    调用一个对象的一个方法,以另一个对象替换当前对象。 
    
        call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 
    
    参数 
    thisObj 
    可选项。将被用作当前对象的对象。 
    
    arg1, arg2,  , argN 
    可选项。将被传递方法参数序列。 
    
    说明 
    call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。 
    
    如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。</SPAN>  

    说简单一点,这两函数的作用其实就是更改对象的内部指针,即改变对象的this指向的内容。这在面向对象的js编程过程中有时是很有用的。下面以apply为例,说说这两个函数在 js中的重要作用。如:

    <SPAN style="FONT-SIZE: 18px"> function Person(name,age){   //定义一个类  
            this.name=name;     //名字  
            this.age=age;       //年龄  
            this.sayhello=function(){alert(this.name)}; 
        } 
        function Print(){            //显示类的属性  
            this.funcName="Print"; 
            this.show=function(){ 
                var msg=[]; 
                for(var key in this){ 
                    if(typeof(this[key])!="function"){ 
                        msg.push([key,":",this[key]].join("")); 
                    } 
                } 
                alert(msg.join(" ")); 
            }; 
        } 
        function Student(name,age,grade,school){    //学生类  
            Person.apply(this,arguments);//比call优越的地方  
            Print.apply(this,arguments); 
            this.grade=grade;                //年级  
            this.school=school;                 //学校  
        } 
        var p1=new Person("卜开化",80); 
        p1.sayhello(); 
        var s1=new Student("白云飞",40,9,"岳麓书院"); 
        s1.show(); 
        s1.sayhello(); 
        alert(s1.funcName);</SPAN>  

    另外,Function.apply()在提升程序性能方面有着突出的作用:
    我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所有参数中的最大值。
    比如

    <SPAN style="FONT-SIZE: 18px">alert(Math.max(5,8));   //8  
        alert(Math.max(5,7,9,3,1,6));   //9  
    
        //但是在很多情况下,我们需要找出数组中最大的元素。  
    
        var arr=[5,7,9,1]; 
        //alert(Math.max(arr));    // 这样却是不行的。NaN  
    
        //要这样写  
        function getMax(arr){ 
            var arrLen=arr.length; 
            for(var i=0,ret=arr[0];i<arrLen;i++){ 
                ret=Math.max(ret,arr[i]); 
            } 
            return ret; 
        } 
    
        alert(getMax(arr)); //9  
    
        //换用apply,可以这样写  
        function getMax2(arr){ 
            return Math.max.apply(null,arr); 
        } 
    
        alert(getMax2(arr)); //9  
    
        //两段代码达到了同样的目的,但是getMax2却优雅,高效,简洁得多。  
    
        //再比如数组的push方法。  
        var arr1=[1,3,4]; 
        var arr2=[3,4,5]; 
        //如果我们要把 arr2展开,然后一个一个追加到arr1中去,最后让arr1=[1,3,4,3,4,5]  
        //arr1.push(arr2)显然是不行的。 因为这样做会得到[1,3,4,[3,4,5]]  
    
        //我们只能用一个循环去一个一个的push(当然也可以用arr1.concat(arr2),但是concat方法并不改变arr1本身)  
    
        var arrLen=arr2.length; 
        for(var i=0;i<arrLen;i++){ 
            arr1.push(arr2[i]); 
        } 
    
        //自从有了Apply,事情就变得如此简单  
    
        Array.prototype.push.apply(arr1,arr2); //现在arr1就是想要的结果</SPAN>  

     5、this的指向问题

            function Person(name,age,sex) {
                this.name = name;
                this.age = age;
                this.sex = sex;
                this.showinfo = function () {
                    alert('name:' + name + "age:" + age + "sex:" + sex)
                }
            }


    this指向当前的环境。现在结合案例来说

    1、当前的环境指向person,所以person就是this

    var person=new Person('马良','22','男');

    person.showinfo()

    2、Person('马良','22','男')

    window.showinfo()

    当前的环境指向window,所以window能调用showinfo

    3、使用call或者apply改变this的指向

    var o={};

    Person.call(o,"马良",22,"男");

  • 相关阅读:
    Linq-单条数据删除
    斐波那契额数列及青蛙跳台阶问题
    旋转数组的最小数字
    扑克牌的顺子
    qsort(),sort()排序函数
    从尾到头打印链表
    查找链表中倒数第k个结点
    左旋转字符串
    数组前半部分和后半部分有序的全排序
    二元树中和为某一值的所有路径
  • 原文地址:https://www.cnblogs.com/sexintercourse/p/5679303.html
Copyright © 2011-2022 走看看