分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

js面向对象继承

发布时间:2023-09-06 01:51责任编辑:傅花花关键词:js面向对象

                      js的面向对象变成其实是基于对象的开发(js是没有累的概念);

1:任何基于对象的变成都有三个特征,抽象封装,继承,多态。

  1 抽象封装:抽象指抽取对象的属性和行为(方法),然后有机的结合在一起,这个过程叫做封装,结果就是只对外提供接口(通过this创建共有属性共有方法),而隐藏内部的实现(共有方法可以调用私有的方法)。

  2 :继承:子类实现拥有父类的属性和方法的过程称之为继承;常用的继承凡事如下两种。

      1:call或者apply    

        function F(x){

 

            this.x=x

 

            this.sayX=function(){

 

             alert(this.x)

 

            }

 

           }

           function Y(x){

 

            F.call(this,x);

 

           //F.apply(this,agruments);

 

             }

 

           var t=new Y(2);

 

          alert(t.sayX())//2

 

        注意??:call apply是无法调用

        

        function F(x){

            this.x=x

          }

          F.prototype.sayX=function(){

 

              alert(this.x)

 

          }

 

         function Y(x){

 

            F.call(this,x);

             }

 

           var t=new Y(2);

 

          alert(t.sayX())//new_file.html?__hbt=1524886368955:91 Uncaught TypeError: t.sayX is not a function

 

       2 原型链继承

       function Person(name, sex){

            this.name=name;

            this.sex=sex;

      }

        Person.prototype.showName=function(){

            alert(this.name);

       }

        Person.prototype.showSex=function(){

              alert(this.sex);

      }

        function worker(name,sex,job){

            this.job=job;

          Person.apply(this,arguments)//获取公有的属性

       }

        worker.prototype=new Person()

        worker.prototype.sayJob=function(){

    ?      alert(this.job)

        }

        var n=new worker(‘xxw‘,‘nan‘,‘chenguxyuan‘);

        console.log(n)

        alert(n.showName())//xxw

        alert(n.showSex())//nan

        alert(n.sayJob())//chenguxyuan

3 多态

      js 天然支持多态度

       通过判断参数argumrnts还有若数据类型,赋值什么数据就是什么数据

 

    

    

js面向对象继承

原文地址:https://www.cnblogs.com/ypwei/p/8966822.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved