|
 
- 注册时间
- 2005-3-9
- 威望
- 1952
- 阅读权限
- 150
- 积分
- 4347
- 帖子
- 1577
- 精华
- 9
- UID
- 24714
- 状态
- 当前离线
|
winter答对咯 ^^
再来个更清晰的例子...
- <script>
- function Polygan()
- {
- var m_points = [];
- m_points = Array.apply(m_points, arguments);
- function GETTER(){};
- GETTER.prototype = m_points[0];
- this.firstPoint = new GETTER();
- this.length = {
- valueOf : function(){return m_points.length},
- toString : function(){return m_points.length}
- }
- this.add = function(){
- m_points.push.apply(m_points, arguments);
- }
- this.getPoint = function(idx)
- {
- return m_points[idx];
- }
- }
- var p = new Polygan({x:1, y:2},{x:2, y:4},{x:2, y:6});
- alert(p.length);
- alert(p.firstPoint.x);
- alert(p.firstPoint.y);
- p.firstPoint.x = 100; //不小心写了它的值
- alert(p.getPoint(0).x); //不会影响到实际的私有成员
- delete p.firstPoint.x; //恢复
- alert(p.firstPoint.x);
- </script>
复制代码运行代码另存代码 |
|