zoukankan      html  css  js  c++  java
  • javascript 实现继承

    本文记录 prototype constructor  实现javascript继承。

    //1:object 对象
    //2:function 对象
    //2:function Prototype 模式
    //拷贝,深拷贝和浅拷贝
    //prototype constructor 方式 ,改变原型,定向构造函数

    直接上代码,

    <html>
    <head>
      <meta charset="utf-8" />
      <title>JS OO</title>
      <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
      <meta content="telephone=no" name="format-detection" />
      <meta name="apple-mobile-web-app-capable" content="yes" />   
    </head>
    <body>  
        
        <script>
            //1:object 对象
            //2:function 对象
            //2:function Prototype 模式
            //拷贝,深拷贝和浅拷贝
            //prototype constructor 方式 ,改变原型,定向构造函数
            
            function person(name,sex){
                this.name=name;
                this.sex= sex;
            }
            person.constructor={};
            person.prototype={
                getName:function(){
                    return this.name;
                },
                getSex:function(){
                    return this.sex;
                },
                age:19
            } 
            //哪国人对象 
            function nationer(name,sex,nation){
                this.name=name;
                this.sex=sex;
                this.nation=nation;
            }
            nationer.prototype=new person(); 
            nationer.prototype.constructor=nationer;
            nationer.prototype.getNation=function(){
                return this.nation;
            }
            //哪国哪省人
            function proviceor(name,sex,nation,provice){
                this.name=name;
                this.sex=sex;
                this.nation=nation;
                this.provice=provice;
            }
            proviceor.prototype=new nationer();
            proviceor.prototype.constructor=proviceor;
            proviceor.prototype.getProvice=function(){
                return this.provice;
            }
            
            var hb = new proviceor('zc','male','china','hb');
        </script>
    </body>
    </html>

    结构:

    作者:zc
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    小老虎CSDN博客流量分析
    C#中字符串的内存分配与驻留池
    最简单的基于FFmpeg的封装格式处理:视音频分离器(demuxer)
    HDU 3037 Saving Beans(Lucas定理的直接应用)
    Linux中IRC通讯工具Pidgin的基本用法
    jQuery整理笔记八----jQuery的Ajax
    Android ServiceManager启动
    C++开源码项目汇总
    虚拟现实游戏的十大误区
    适用android的MVP:怎样组织展示层
  • 原文地址:https://www.cnblogs.com/jmzs/p/4932413.html
Copyright © 2011-2022 走看看