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

JS private 如何使用

  •  
  •   kaiwei · Feb 27, 2016 · 3866 views
    This topic created in 3754 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
        2
    Septembers  
       Feb 27, 2016 via Android
    您认真看您发的文章了吗? secret 才是私有的啊
    kaiwei
        3
    kaiwei  
    OP
       Feb 27, 2016
    @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
        4
    xcodebuild  
       Feb 27, 2016 via Android
    @kaiwei 你访问的是 member ,不是这三个的任何一个,有任何问题么?
    yuriko
        5
    yuriko  
       Feb 27, 2016
    原谅我放荡不羁的笑点……
    jsonline
        6
    jsonline  
       Feb 27, 2016 via Android
    为什么要把其它语言的概念搬到 JS
    jkeylu
        7
    jkeylu  
       Feb 27, 2016
    ES6 中 closure + Symbol 就能实现 private 的效果
    neoblackcap
        8
    neoblackcap  
       Feb 28, 2016 via iPhone
    @kaiwei 如 @codefalling 所说你访问的是 member ,原文只说 param, secret, that 三个变量是私有的而已
    xuzicn
        9
    xuzicn  
       Feb 28, 2016
    额外插一句, JS 没有 private 这一说,想要实现数据保护经常引入额外的麻烦
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   901 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 19:32 · PVG 03:32 · LAX 12:32 · JFK 15:32
    ♥ Do have faith in what you're doing.