推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
kaiwei

JS private 如何使用

  •  
  •   kaiwei · Feb 27, 2016 · 4309 views
    This topic created in 3924 days ago, the information mentioned may be changed or developed.
    最近看了道爷的 2001 文章
    http://www.crockford.com/javascript/private.html
    在自己的机器上跑,并没有 private 的效果啊。
    ```javascript
    function Container(param) {
    this.member = param;
    var secret = 3;
    var that = this;
    }
    var myContainer = new Container('abc');
    console.log(myContainer.member); // 依然会打印 abc
    myContainer.member = 'efg';
    console.log(myContainer.member); // 会打印 efg
    ```
    求解。多谢!
    9 replies  •  2016-02-28 17:36:07 +08:00
    Septembers
    Septembers
    Feb 27, 2016 via Android
    2
    您认真看您发的文章了吗? secret 才是私有的啊
    kaiwei
    kaiwei
    OP
    Feb 27, 2016
    3
    @Septembers 在 Private 那个标题下面:
    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. They are accessible to private methods. Private methods are inner functions of the constructor.
    他说,三个都是不能 accessible to the outside 。
    xcodebuild
    xcodebuild
    Feb 27, 2016 via Android
    4
    @kaiwei 你访问的是 member ,不是这三个的任何一个,有任何问题么?
    yuriko
    yuriko
    Feb 27, 2016
    5
    原谅我放荡不羁的笑点……
    jsonline
    jsonline
    Feb 27, 2016 via Android
    6
    为什么要把其它语言的概念搬到 JS
    jkeylu
    jkeylu
    Feb 27, 2016
    7
    ES6 中 closure + Symbol 就能实现 private 的效果
    neoblackcap
    neoblackcap
    Feb 28, 2016 via iPhone
    8
    @kaiwei 如 @codefalling 所说你访问的是 member ,原文只说 param, secret, that 三个变量是私有的而已
    xuzicn
    xuzicn
    Feb 28, 2016
    9
    额外插一句, JS 没有 private 这一说,想要实现数据保护经常引入额外的麻烦
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   980 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 19:24 · PVG 03:24 · LAX 11:24 · JFK 14:24
    ♥ Do have faith in what you're doing.