zoukankan      html  css  js  c++  java
  • 简单模拟javaScript面向对象

    <html>

    <head>

    <script type="text/javascript">

      if (!Object.create) {

        Object.create = function (o) {

           function F() {}

          F.prototype = o;

          return new F();

        };

      }

     function alertt(){

           ca = new cat();

           alert(ca.name);

           dog1=Object.create(Dog);

           dog1.shout();

           pig=Pig.createNew();

           pig.shout();

           spig = SmallPig.createNew();

           spig.shout();

    }

    //方法一

    Pig = {

                  job:"eat";//全局静态变量,可被多个对象共享

        createNew: function(){

          var pig = {};

                         sound = "herher";//直接定义是私有

          pig.name = "BigPig";

          pig.shout = function(){ alert(sound); };

          return pig;

        }

    };

    //继承

    SmallPig = {

           createNew: function(){

                         var pig = Pig.createNew();

                         pig.name = "SmallPig";

                         pig.shout = function(){ alert("wuher wuher"); };

          return pig;

           }

    }

    //方法二

    Dog={

           shout:function(){alert("wangwang");}

    }

    //方法三

    function cat(){

           this.name = "miaomiao";

    }

    </script>

    </head>

    <body>

    <button id="b01" type="button" onclick="alertt()">动物叫</button>

    </body>

    </html>

  • 相关阅读:
    [leetcode] N-Queens
    [leetcode] Minimum Path Sum
    [leetcode] Longest Valid Parentheses
    习题9-1 时间换算
    long常量和long long常量
    整数占用空间
    来到地球多久啦
    习题8-10 输出学生成绩
    习题8-7 字符串排序
    习题8-9 分类统计各类字符个数
  • 原文地址:https://www.cnblogs.com/flying607/p/3495361.html
Copyright © 2011-2022 走看看