zoukankan      html  css  js  c++  java
  • js创建对象

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <title>Document</title>
    </head>
    
    <body>
    
      <script>
        // 1  new Object()
        // var hero = new Object();  // 空对象
        // hero.blood = 100;
        // hero.name = '刘备';
        // hero.weapon = '剑';
    
        // hero.attack = function () {
        //   console.log(this.weapon + ' 攻击敌人');
        // }
    
        // 2 对象字面量
        // var hero = {};   // 空对象
        // var hero1 = {
        //   blood: 100,
        //   name: '刘备',
        //   weapon: '剑',
        //   attack: function () {
        //     console.log(this.weapon + ' 攻击敌人');
        //   }
        // }
    
        // // hero.attack();
        // var hero2 = {
        //   blood: 100,
        //   name: '关羽',
        //   weapon: '刀',
        //   attack: function () {
        //     console.log(this.weapon + ' 攻击敌人');
        //   }
        // }
        // 
        // 3 工厂函数 创建多个对象
        function createHero(name, blood, weapon) {
          var o = new Object();
          o.name = name;
          o.blood = blood;
          o.weapon = weapon;
          o.attack = function () {
            console.log(this.weapon + ' 攻击敌人');
          }
          return o;
        }
    
        var hero = createHero('刘备', 100, '');
        var hero1 = createHero('关羽', 100, '');
        // console.log(typeof hero);
        console.log(hero instanceof createHero);
    
    
        var arr = [];
        console.log(arr instanceof Array);
      </script>
    </body>
    
    </html>
  • 相关阅读:
    JAVA 线程安全与同步机制
    JAVA 多线程
    el-table 宽度自适应bug
    详解迭代器Iterator
    理解基本类型的溢出
    理解classpath
    I/O(一):基础知识
    C++: 智能指针
    C++: 值类别与移动语义基础
    CUDA 架构与编程概述
  • 原文地址:https://www.cnblogs.com/jiumen/p/11429940.html
Copyright © 2011-2022 走看看