js闭包中的沙箱模式

    xiaoxiao2021-03-25  127

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Title</title> </head> <body> </body> <script>     //闭包实现模块化:沙箱模式      -->设计模式中的模块模式     //模块化:就是一个能够实现完整功能的独立体(不能被外界污染)     //一般来说,在开发中会将面向对象和模块化进行混合使用     //--->使用技巧:把一个功能当成模块,在模块下面使用面向对象来实现     var Person=(function(){         //fn是一个局部函数,用户不可以随意修改         function fn(){             console.log("fn")         }         function Person(){         }         Person.prototype={             constructor:Person,             s1:function(){                 fn();             },             s2:function(){             }         }         return Person;//Person是一个函数,可以访问到这一次执行环境中产生的fn Person的内存地址         //由于是一个自执行函数,外层函数只会执行一次,导致了Person是唯一的     })()     var p1=new Person();     var p2=new Person();     //delete Person.prototype.s1; </script> </html>
    转载请注明原文地址: https://ju.6miu.com/read-8621.html

    最新回复(0)