作者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