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;

        }

  • 相关阅读:
    JS-函数
    JS-数组
    JS-2
    课堂小技巧
    CSS利用filter/opacity实现背景透明
    [技巧心得] 背景半透明最佳实践
    Cadence Orcad 无法启动出现Capture.exe找不到cdn_sfl401as.dll问题
    正则表达式
    python小项目之头像右上角加数字
    Django开发之路 二(django的models表查询)
  • 原文地址:https://www.cnblogs.com/ai3xiaoyi/p/3587131.html
Copyright © 2011-2022 走看看