javascript 隐藏_用JavaScript隐藏您的私人

javascript 隐藏

JavaScript Privates

Believe it or not, there are a lot of people who strongly dislike JavaScript. For one reason or another, they simply can't stand the perceived lack of security and structure within the language. One of those perceived weaknesses is lack of the private designations for variables within structures. They have a point; if you stick a property on an object, or create d global variables, it's fair game for anyone. There is, however, a few very simple patterns you can use within JavaScript to keep access to a given object or variable private!

信不信由你,有很多人非常不喜欢JavaScript。 出于某种原因,他们根本无法忍受语言中缺乏安全性和结构性的感觉。 这些可察觉的弱点之一是缺乏对结构内变量的专用名称。 他们有道理; 如果您在对象上添加属性或创建d全局变量,那么对任何人来说都是公平的游戏。 但是,可以在JavaScript中使用一些非常简单的模式来保持对给定对象或变量的私有访问!

有“班级” (With "Classes")

You probably know that true classes don't exist in JavaScript, but using Function and prototypes we can simulate them pretty damn well. Using this pattern, you can also simulate private variables:

您可能知道真正的类在JavaScript中不存在,但是使用Function和原型我们可以很好地模拟它们。 使用此模式,您还可以模拟私有变量:


function MyClass(parameter) {
	
	// Create the private var
	var privateVar = 8,
		self = this;
	self.parameter = parameter
	
	// Function that handles the private
	function handlePrivate() {
		return privateVar ? privateVar-- :  false;
	}
	
	// Public method that calls the handler
	// Returns parameter until it's been accessed 8 times
	this.getPrivate() {
		return handlePrivate() ? self.parameter : null;
	}

}


Privates are made within the constructor and only the constructor. Adding privileged methods within the constructor give access to privates. In the sample above, the value is accessible only by the privileged method.

私有是在构造函数中进行的,并且仅在构造函数中进行。 在构造函数中添加特权方法可以访问私有数据。 在上面的示例中,该值只能由特权方法访问。

带封闭 (With Closures)

The pattern centers around a closure with a scoped variable, returning an object or function with access to the variable. Here's a simple example:

该模式以带有范围变量的闭包为中心,返回有权访问该变量的对象或函数。 这是一个简单的例子:


var accessor = (function() {
	// Create the private var
	var privateVar = "You Can't See Me, ";
	
	// Some other functionality here that periodically changes privateVar, because it has access to it
	// [ more code here ]
	
	// Return the accessor
	return function(name) {
		return privateVar + name;
	};
})();

// Use!
// currentValue = accessor("David");  "You Can't See Me, David"


The privateVar variable is not available out side the closure so there's no way to access it directly. We do, however, have a define function with access to the variable and return that from the closure. Thus, the variable stays private, you can access its value, but no direct access is allowed. You could also return an object with methods for modifying the protected variable:

privateVar变量在闭包之外不可用,因此无法直接访问它。 但是,我们确实具有可以访问变量的define函数,并从闭包中返回该函数。 因此,变量保持私有状态,您可以访问其值,但不允许直接访问。 您还可以返回带有用于修改受保护变量的方法的对象:


var accessor = (function() {
	
	// The "private"
	var val = 9;
	
	// An object for getting and setting
	return {
		get: function() {
			return val * 3;
		},
		set: function(s) {
			val = s;
		}
	};
})();


Of course, with the pattern above, a user could reassign the get method with something like:

当然,使用上述模式,用户可以使用以下方式重新分配get方法:


accessor = function(n) { return "My custom string, " + n; }


..so be aware of that. Usually any functionality that handles private information stays within the closure, preventing issues with users overriding methods. Something like this:

..因此请注意。 通常,任何处理私人信息的功能都保留在闭包中,以防止用户覆盖方法的问题。 像这样的东西:


var accessor = (function() {
	
	var permissions = {};
	
	function handleResult(perm) {
		// Do something with the result
	}
	
	return function(permissionToCheck, callback) {
		if(permissions[permissionToCheck]) != undefined) {
			callback(permissions[permissionToCheck]);
		}
		else {
			$.post({
				url: "/permissions"
				data: {
					perm: permissionToCheck
				}
			}).success(function(res) {
				callback(handleResult(res));
			});
		}
	};
	
})();


There's a certain beauty in the simplicity of JavaScript; don't write it off because it doesn't provide the same explicit structures as other languages!

JavaScript的简单性具有一定的美感。 不要撇开它,因为它没有提供与其他语言相同的显式结构!

翻译自: https://davidwalsh.name/javascript-private

javascript 隐藏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值