zoukankan      html  css  js  c++  java
  • Js定义类或对象

    一、工厂方法

     1.原始

      var stu = new Object();

      stu.name = "张三";

      stu.age = 10;

      stu.getName = function(){

          return this.name;

    }

    2.解决

     function createStudent(){

       var stu = new Object();

       stu.name = "张三";

       stu.age = 10;

       stu.getName = function(){

          return this.name;

    }

    return stu;

    }

    3.传参数

    function createStudent(name,age){

        var stu = new Object();

        stu.name = name;

        stu.age = age;

        stu.getName = function(){

            return this.name;

        }

        return stu;

    }

    二、构造函数与原型

       1.构造函数

        function Student(name,age){

          this.name = name;

          this.age = age;

          this.getName = function(){

              return this.name;

        }

    }

       2.原型

          function Student(){}

          Student.prototype.name = "张三";

          Student.prototype.age = 10;

          Student.prototype.getName = function(){

               return this.name;

        }

      3.构造函数+原型

        function Student(name,age){

          this.name = name;

          this.age = age;

        }

        Student.prototype.getName = function(){

           this.name;

        }

  • 相关阅读:
    java基础语法
    MySQL5.7常用命令
    wireshark抓包分析---TCP/IP协议
    MySQL安全管理
    MySQL触发器
    MySQL存储过程和游标
    mysql实现远程登录
    Java中遍历Map对象的4种方法
    SSM-CRUD
    SSM整合-配置文件
  • 原文地址:https://www.cnblogs.com/ai3xiaoyi/p/3587131.html
Copyright © 2011-2022 走看看