2012-07-17 54 views
7

在Java编程语言中,专用关键字用于数据隐藏 - 标记为专用的字段或方法在类或子类之外不可见。隐藏在Javascript中的数据

javascript是如何实现的?

+6

私人领域是通过重大的受虐行为实现的。 – biziclop 2012-07-17 15:29:21

+1

与大多数JavaScript相关的东西一样,道格拉斯克罗克福德在该语言中有一些有趣的[关于私人成员的想法](http://www.crockford.com/javascript/private.html)。 – maerics 2012-07-17 15:29:44

回答

8

在JavaScript中标准的方法是使用模块模式如下所示..

var testModule = (function() { 

    var myPrivateVar = 0; 

    var myPrivateMethod = function (someText) { 
     console.log(someText); 
    }; 

    return { 

     myPublicVar: "foo", 

     myPublicFunction: function (bar) { 
      myPrivateVar++; 
      myPrivateMethod(bar); 
     } 

    }; 
})(); 

用法:在上面的代码被返回的对象,其包含一个变量(myPublicVar)和功能(myPublicFunction)。在此函数内部,您可以访问内部变量(myPrivateVar)和内部函数(myPrivateMethod),但不能从外部访问。

var mod = new testModule(); 
mod.myPublicFunction(param); 
+0

伟大的工程和由道格拉斯克罗克福德本人推荐:) – awendt 2012-07-17 15:29:40

+0

你可以提供一些解释,以什么发生在你的代码,以及这是如何涵盖数据隐藏在JavaScript - 谢谢 – oneiros 2012-07-17 17:10:48

+1

查找“JavaScript闭包”,以了解如何保持数据远离公共范围。另一个函数内的任何函数都会创建一个闭包。内部函数可以“记住它周围的东西”。上面的例子演示了一个函数EXECUT IMYEDIATELY with(),从而返回一些内部引用到名为“testModule”的新对象。 – Billbad 2012-07-17 19:42:22

1

这一切都是通过范围界定来实现的。

var MYCLASS = function(){ 

    var priv_var = 0; //private var 

    this.addToVar = function(){ 
     priv_var++; 
    } 

    this.showVar = function(){ 
     return priv_var; 
    } 

} 

var mc = new MYCLASS; 

mc.addTovar(); 
alert(mc.showVar()); //"1" 
0
"use strict"; 

var Person = function (fName, lName) { 

    var firstName = fName || "AN", lastName = lName || "Other"; 

    var fullName = function() { 
     return firstName + ' ' + lastName; 
    }; 

    return { 
     setName: function (fName, lName) { 
      firstName = fName; 
      lastName = lName; 
     }, 

     getFullName: function() { 
      return fullName(); 
     } 
    }; 

} 

var p = new Person("Your", "name"); 

console.log(p.getFullName()); 
1

下面是一个简单的API,你可能会喜欢。它有三个功能:Key(一个构造函数),KeepSecret,FetchSecret。

你可以做的是将一个具有秘密守护者的对象作为一个属性。然后对象可以“随身携带”数据,但是访问对象但不知道密钥的代码无法访问隐藏的数据。

/** 
* Example usage: 
* 
* Create a key 
* 
var mykey = Key(); 

* 
* Keep a secret 
* 
var mykeeper = KeepSecret(mykey, 42); 

* 
* Fetch the secret 
* 
var answer = FetchSecret(mykey, mykeeper); 

* 
* 'answer' will then contain 42 
*/ 

(function(Namespace, Code) { return Code(Namespace); })(
    /* Choose the namespace for Key, KeepSecret, FetchSecret */ 
    this, 

    function(Namespace) { 

/* 
* Simply so that we can use "Key" as both a type-name 
* and a parameter-name 
*/ 
var ikey; 

/** Constructor for a key */ 
function Key() { 
    if (!(this instanceof Key)) 
     return new Key(); 
    } 

/* Same key constructor */ 
ikey = Key; 

/** 
* Hide a secret using a key 
* 
* @param Key 
* The key to lock away the secret with 
* 
* @param Secret 
* The secret to be locked away 
* 
* @return 
* A function which hides the secret inside 
*/ 
function KeepSecret(Key, Secret) { 
    /* The function can access itself */ 
    var closure; 

    if (!(Key instanceof ikey)) 
     throw "KeepSecret: Invalid key"; 

    closure = function(key) { 
     /* If we were not passed the key, authenticate */ 
     if (key !== Key) { 
      Key.keeper = closure; 
      return; 
      } 

     /* The caller knew the key, so reveal the secret */ 
     return Secret; 
     } 
    return closure; 
    } 

/** 
* Use a key and a function to reveal the secret that function keeps 
* 
* @param Key 
* The key for unlocking the secret 
* 
* @param Keeper 
* The function keeping the secret 
* 
* @return 
* The secret, if the key unlocks it 
*/ 
function FetchSecret(Key, Keeper) { 
    /* Tracks authentication */ 
    var closure; 

    if (!(Key instanceof ikey) || !(Keeper instanceof Function)) 
     throw "FetchSecret: Invalid parameter(s)"; 

    /* Ask the keeper to authenticate */ 
    Keeper(); 

    /* Note the authenticated function */ 
    closure = Key.keeper; 

    /* Clear the authentication */ 
    delete Key.keeper; 

    /* Did the keeper prove that they know the key? */ 
    if (closure !== Keeper) 
     /* No */ 
     return; 

    /* They know the key. Show we know the key, too */ 
    return closure(Key); 
    } 

Namespace.Key = Key; 
Namespace.KeepSecret = KeepSecret; 
Namespace.FetchSecret = FetchSecret; 
return true; 

    });