GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Javascript面向对象编程 · Issue #32 · Wscats/articles Closed Labels DescriptionJS面向对象的程序设计,JS在很早以前一直被认为毫无章法的语言,大部分初学者写的时候都是运用面向过程的思维来编写JS的(一页下来就是一个function接着另一个function的写法) 其实JS中function就是一个对象,如下面的代码 var HelloWscats = function(){ console.log("wscats is not cat"); } var _init = function(){ var obj = new HelloWscats(); } _init();//log:wscats is not cat 此时我们就可以调用_init 方法后打印 其实HelloWscats 就是我们所说的创建对象中的其中一种构造函数模式, var _init2 = function(param){ var obj = new Object(); var obj = {} obj.action = "b"; Object.defineProperty(obj, "name", { configurable: false, writable: false, value: "Wscats", }); obj.name = "wscats";//无效 delete obj.name;//无效 console.log("wscats is not cat"); return obj; } _init2("wscats is not cat"); 我们稍微改一下,执行_init2函数的时候,相当于创建对象中的另一种我们说的工厂模式,创建对象交给一个工厂方法(_init2)来实现,可以传递参数,这里创建对象的方式跟构造函数方式的区别在于 再写一个简单的例子来说明他们的区别,如下 var Constructor = function() { console.log("wscats is not cat from constructor"); } var objFromConstructor = new Constructor(); var Factory = function() { var obj = new Object(); console.log("wscats is not cat from factory"); return obj; } var objFromFactory = Factory(); 上面就是一个最简单的对象,不过既然是对象,肯定是要赋予它属性和方法的 var HelloWscats = function(){ this.action = "Code"; console.log("wscats is not cat"); } HelloWscats.prototype = { name: "wscats", say: function(){ console.log("Wscats is not cat"); } } HelloWscats.prototype.skill = "Good Cat"; var _init = function(){ var obj = new HelloWscats(); obj.say(); console.log(obj); } _init(); 这里我们看到了先是执行了构造函数中的 上面有一定注意的是,如果我实例化了HelloWscats对象 var _init = function(){ var obj = new HelloWscats(); obj.say(); obj.action = "b"; console.log(obj); } 我们可以改变,这个对象的action属性 var HelloWscats = function(){ var privateVvariables = "I am ur cat"; this.getPrivate = function(){ console.log(privateVvariables); } this.action = "Code"; console.log("wscats is not cat"); } HelloWscats.prototype = { name: "wscats", say: function(){ console.log("Wscats is not cat"); } } HelloWscats.prototype.skill = "Good Cat"; var _init = function(){ var obj = new HelloWscats(); obj.say(); obj.action = "b"; obj.getPrivate(); console.log(obj); } _init(); 下面我可以继续在构造函数里面写一个 this.getPrivate = function(){ console.log(privateVvariables); } 它的巧妙之处其实在于这个构造函数的作用域,只能在类的构造函数里面进行访问该私有变量 var _init = function() { var obj = new HelloWscats(); obj.say(); obj.action = "b"; obj.getPrivate(); Object.defineProperty(obj, "name", { configurable: false, writable: false, value: "Wscats", }); obj.name = "wscats";//无效 delete obj.name;//无效 console.log(obj); } 上面我们一直用 Object.defineProperty(obj, "name", { configurable: false, writable: false, value: "Wscats", }); defineProperty方法能修改对象的属性它接收三个参数:属性所在对象,属性名和一个描述符对象(必须是:configurable、enumberable、writable和value,可设置一个或多个值)
正如我写obj的name属性 obj.name = "wscats";//无效 delete obj.name;//无效 删除该对象的name属性值和修改name属性值也变得无效 Activity
MetadataMetadataAssigneesLabelsTypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |