zoukankan      html  css  js  c++  java
  • js设计模式=封装

    js封装案例【1】

    <script>
    var Book = function(num){
        var num;//类私有变量
        var name;//类私有变量
        function check(){};//类私有方法
        this.checkName = function(){}//特权方法
    }
    Book.prototype.checkNum=10;//公共属性
    Book.prototype.display=function(){//公共方法
        console.log("this is display");
    }
    Book.isChinese=true;//类似静态功能属性
    Book.checkInfo=function(){}//类似静态功能方法
    
    var b1 = new Book();
    b1.display();
    </script>

     js使用闭包完成封装【2】

    <script>
    var Book =(function(){
        //静态私有变量
        var bookNum=0;
        //静态私有方法
        function checkBook(name){
            console.log(name);
        }
        //使用了闭包
        return function(newId,newName,newPrice){
            //私有变量
            var name,price;
            //私有方法
            function checkId(){
                console.log(bookNum);
            }
            //特权方法=和外面交互
            this.getName = function(){
                console.log(this.name);
            }
            this.getPrice= function(){
                console.log(this.price);
            }
            this.setPrice= function(newPrice){
                this.price = newPrice;
            }
            this.setName = function(newName){
                this.name = newName;
            }
            this.copy = function(){}
            this.setName(newName);
            this.setPrice(newPrice);
            bookNum++;
            checkId();
            checkBook(newName);
        }
    })();
    //静态属性和方法
    Book.prototype={
        isjsBook:false,
        display:function(){}
    };
    
    var book = new Book(1,'jsBook',30);
    book.getName();
    book.getPrice();
    </script>

      js使用闭包完成封装【3】

    <script>
     var Book = (function(){
         //静态私有变量
         var bookNum=0;
         //静态私有方法
         function checkBook(name){}
         function book (newId,newName,newPrice){
             //私有变量
             var book,price;
             //私有方法
             function checkId(){}
             //特权方法&属性
             this.getName = function(){}
             this.getPrice = function(){}
             this.setName = function(){}
             this.setPrice = function(){}
             this.copy=function(){}
             //构造方法和属性
             this.id = newId;
             this.setName(newName);
             this.setPrice(newPrice);
         }
         //构造原型
         book.prototype={
             isJSBook:true,
             display:function(){console.log()}
         };
         return book;
     })();
     var book = new Book(1,'jsbook',20);
     book.display();
     //备注这样看来是一个完整的整体
    </script>
  • 相关阅读:
    Python List comprehension列表推导式
    Git
    Python 默认参数 关键词参数 位置参数
    Blog List
    【论文笔记】DeepOrigin: End-to-End Deep Learning for Detection of New Malware Families
    【论文笔记】Malware Detection with Deep Neural Network Using Process Behavior
    Scala入门 【1】
    RocketMQ与Kafka对比(18项差异)
    Kafka简介
    Spark存储管理(读书笔记)
  • 原文地址:https://www.cnblogs.com/zh718594493/p/12392941.html
Copyright © 2011-2022 走看看