作者BBSealion (海狮)
看板Ajax
标题Re: [问题] javascript 的 private 属性
时间Fri Feb 22 17:29:46 2013
Private members are made by the constructor. Ordinary vars and parameters of
the constructor becomes the private members.
function Container(param) {
this.member = param;
var secret = 3;
var that = this;
}
This constructor makes
three private instance variables: param, secret, and
that. They are attached to the object, but they are not accessible to the
outside,
nor are they accessible to the object's own public methods.
by
http://javascript.crockford.com/private.html
---
我知道private的用意就是保护变数不被乱动
但另一个他该有的功能是:
在自己的method中能被使用
不然我为了自己要用他就得开getter给他,如此变成您说的无意义了
我想js的逻辑跟C应该不太一样,所以有点困惑中
但应该有个对应的做法
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.133.61.45
1F:推 lausai:大师的作法符合的严格的物件导向要求 但却牺牲了效能跟简洁 02/22 17:44
2F:→ lausai:所以我觉得折衷的底线宣告法 应该是现在最好的方式 还有一 02/22 17:45
3F:→ lausai:般提到 getter/setter 都会想到是 1.public 2.called from 02/22 17:46
4F:→ lausai:outside client 所以你提到 getter/setter 会让人有点误会 02/22 17:47
5F:→ lausai:因为网页中你所谓的 setter/getter 是内部使用并不开放 02/22 17:54
6F:→ lausai:大师也没有使用getter/setter 而是以private method 称呼它 02/22 17:55
7F:→ BBSealion:恩了解~ 加底线真的是个很轻松的解法 02/22 18:09