<!DOCTYPE HTML> <HTML> <HEAD> <TITLE> New Document </TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- //结构比较清晰的闭包 window.ChatLog1 = function(){ function _ChatJS(){ this.test = function(){ alert(1); } this.aa = "aa"; this.test2 = function(){ alert(aa); } //将aa定义成常量,youyu4 Object.defineProperty(this, "aa", { writable: false, //不可修改 configurable: false //不可被删除 //value: "aa1" }); } return new _ChatJS(); }(); //传统的闭包形式 var person = { name: "Nicholas", age: 29, job: "Software Engineer", sayName: function(){ alert(this.name); } }; ChatLog1.aa = "bb"; alert(ChatLog1.aa); //--> </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
New Document