zoukankan      html  css  js  c++  java
  • 几种构造对象的方法

    23         //3.自定义构造函数
    24         function getBoy(name,age) {
    25             this.name = name;
    26             this.age = age;
    27             this.yao = function () {
    28                 console.log("跟我龟丞相,一起摇:黑马乱不乱,龟爷说了算");
    29             }
    30         }
    31         var lmg = new getBoy("镇楼神龟",9000);
    32         console.log(lmg);
    33         lmg.yao();
    //注:自定义构造函数的方法,在我写小鸟游戏的时候一直有在使用!!!
     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6     <script>
     7         //1.利用内置构造函数的方式来创建对象
     8         var zyb = new Object();
     9         zyb.name = "绿波";
    10         zyb.age = 40;
    11         console.log(zyb);
    12 
    13         //2.利用工厂模式
    14         function getStu(name,age) {
    15             var stu = new Object();
    16             stu.name = name;
    17             stu.age = age;
    18             return stu;
    19         }
    20         var cc = getStu("程胖",16);
    21         console.log(cc);
    22         
    23         //3.自定义构造函数
    24         function getBoy(name,age) {
    25             this.name = name;
    26             this.age = age;
    27             this.yao = function () {
    28                 console.log("跟我龟丞相,一起摇:黑马乱不乱,龟爷说了算");
    29             }
    30         }
    31         var lmg = new getBoy("镇楼神龟",9000);
    32         console.log(lmg);
    33         lmg.yao();
    34 
    35         //4.在使用内置构造方法的同时,初始化
    36         var lcw = new Object(
    37             {
    38                 name:"梁朝伟",
    39                 age:16,
    40                 say:function () {
    41                     console.log(this.name);
    42                 }
    43             }
    44         );
    45         console.log(lcw);
    46         lcw.say();
    47 
    48         //5.使用对象的字面量来创建对象/可以初始化
    49         var mpy = {
    50             name:"妈卖批",
    51             age:56,
    52             say:function () {
    53                 console.log(this.name);
    54             }
    55         };
    56         var res = Object.prototype.toString.call(mpy);
    57         console.log(res);//object Object
    58         console.log(mpy);
    59         mpy.say();
    60     </script>
    61 </head>
    62 <body>
    63 
    64 </body>
    65 </html>
  • 相关阅读:
    路由器的配置及使用
    OSI与TCP/IP网络模型
    IP地址
    C++中的虚函数
    虚基类
    【idea】jrebel
    JSON(来自ww3school)
    get与post
    Ajax(来自w3school)
    EasyUI——combotree
  • 原文地址:https://www.cnblogs.com/DZzzz/p/8527659.html
Copyright © 2011-2022 走看看