function User( properties ){ for( var i in properties ){ (function( which ){ var p = i; which[ "get" + i ] = function(){ return properties[p]; }; which[ "set" + i ] = function(val){ properties[p] = val; } })(this); }}var user = new User({ name : "Bob", age : 44});console.log(user.name);console.log(user.getname());console.log(user.getage());這是JS面向?qū)ο缶幊虝系囊粋€(gè)例子,我想問問這個(gè)實(shí)際有用處嗎? 雖然我對(duì)閉包什么的略知一點(diǎn)可是這里的 which this val 搞的我好暈 求牛人解釋一下。。。再問一個(gè)Function.prototype.method = function(name, func){ this.prototype[name] = func; return this;};所以name必須是個(gè)string? this.prototype[name]中的this 和 return this 分別指的是?
JAVASCRIPT OOP 閉包 this指代 動(dòng)態(tài)get,set
紅糖糍粑
2019-03-19 18:19:44