Javascript动态的getter / setter为私有属性 [英] Javascript dynamically getter/setter for private properties

查看:168
本文介绍了Javascript动态的getter / setter为私有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我做的。



首先,我做了这个课程:

  function winClass(posX,posY,w,h){
var x = posX || 0;
var y = posY || 0;
var width = w || 0;
var height = h || 0;
}

然后我用getter / setter方法扩展winClass,如下所示:

  winClass.prototype.getX = function(){
return x;
}

winClass.prototype.setX = function(val){
x = val;
}

然后我测试:

  var win1 = new winClass(10,10,100,100); 
document.write(win1.getX());

但是,当我尝试设置'getX'方法时,会出现以下错误:'x未定义。
这是有道理的,因为'x'不在winClass范围内,因此我不知道如何为私有变量设置动态getter / setter方法。



任何想法?

解决方案

getter / setter必须在可以看到私有变量的范围内可以看到这些变量的范围是构造函数的内部。这就是为什么这些变量实际上是私有的。所以,为了给他们设置/获取,你必须把功能放在可以看到它们的范围内。这将工作:

 函数winClass(posX,posY,w,h){
var x = posX || 0;
var y = posY || 0;
var width = w || 0;
var height = h || 0;

this.getX = function(){return(x);}
this.setX = function(newX){x = newX;}
}

var win1 = new winClass(10,10,100,100);
alert(win1.getX()); // alert 10

你可以看到它在这里工作: http://jsfiddle.net/jfriend00/hYps2/



如果你想要一个通用的getter / setter私有,你可以这样做:

 函数winClass(posX,posY,w,h){
var privates = {};
privates.x = posX || 0;
privates.y = posY || 0;
privates.width = w || 0;
privates.height = h || 0;

this.get = function(item){return(privates [item]);}
this.set = function(item,val){privates [item] = val;}
}

var win2 = new winClass(10,10,100,100);
alert(win2.get(x)); // alert 10

而且,如果你想要隐藏这些变量的私有性质,感觉到我(因为你可能会使它们成为标准的实例变量),你可以这样做:

  function winClass (posX,posY,w,h){
var privates = {};
privates.x = posX || 0;
privates.y = posY || 0;
privates.width = w || 0;
privates.height = h || 0;

this.getPrivates = function(){return(privates);}
}

winClass.prototype.getX = function(){
return (this.getPrivates()X。);
}

winClass.prototype.setX = function(newX){
this.getPrivates()。x = newX;
}

示例: http://jsfiddle.net/jfriend00/EKHFh/



当然,这样会废除变量的私有性质没有任何意义上的这样做,因为使他们的常规实例变量将更容易,具有相同的访问控制。



而且,为了完整,这里是正常的实例变量方法,可以自由地将你添加到原型的访问器方法,但变量不是私有的。

  function winClass posX,posY,w,h){
this.x = posX || 0;
this.y = posY || 0;
this.width = w || 0;
this.height = h || 0;
}

winClass.prototype.getX = function(){
return(this.x);
}

winClass.prototype.setX = function(newX){
this.x = newX;
}


I want to create getter/setter methods dyanmically to retrieve private properties.

This is what I did.

First of all, I made the class:

function winClass (posX, posY, w, h) {
  var x = posX || 0;
  var y = posY || 0;
  var width = w || 0;
  var height = h || 0;
}

Then I extended winClass with getter/setter methods, as follows:

winClass.prototype.getX = function () {
  return x;
}

winClass.prototype.setX = function (val) {
  x = val;
}

And then I tested:

var win1 = new winClass (10, 10, 100, 100);
document.write (win1.getX ());

But the following error comes when I try to setup the 'getX' method: 'x is not defined'. It makes sense because that 'x' isn't in the winClass scope but thus I don't know how to setup dynamically getter/setter methods for private variables.

Any ideas?

解决方案

The getter/setters have to be in the scope that can see the private variables and the only scope that can see these variables is the internals of the constructor. That's why these variables are actually private. So, to make setters/getters for them, you have to put the functions in that scope that can see them. This will work:

function winClass (posX, posY, w, h) {
  var x = posX || 0;
  var y = posY || 0;
  var width = w || 0;
  var height = h || 0;

  this.getX = function() {return(x);}
  this.setX = function(newX) {x = newX;}
}

var win1 = new winClass (10, 10, 100, 100);
alert(win1.getX());   // alerts 10

You can see it work here: http://jsfiddle.net/jfriend00/hYps2/.

If you want a generic getter/setter for privates, you could do it like this:

function winClass (posX, posY, w, h) {
  var privates = {};
  privates.x = posX || 0;
  privates.y = posY || 0;
  privates.width = w || 0;
  privates.height = h || 0;

  this.get = function(item) {return(privates[item]);}
  this.set = function(item, val) {privates[item] = val;}
}

var win2 = new winClass(10,10,100,100);
alert(win2.get("x"));    // alerts 10

And, if you want to hack around the private nature of these variables which makes no sense to me (as you might as well make them standard instance variables then), you can do it like this:

function winClass (posX, posY, w, h) {
  var privates = {};
  privates.x = posX || 0;
  privates.y = posY || 0;
  privates.width = w || 0;
  privates.height = h || 0;

  this.getPrivates = function() {return(privates);}
}

winClass.prototype.getX = function() {
    return(this.getPrivates().x);
}

winClass.prototype.setX = function(newX) {
    this.getPrivates().x = newX;
}

Example here: http://jsfiddle.net/jfriend00/EKHFh/.

Of course, this ruins the private nature of the variables so there isn't really any point in doing it this way as making them regular instance variables would be easier and have the same access control.

And, for completeness, here's the normal instance variable method that freely lets you add accessor methods to the prototype, but the variables aren't private.

function winClass (posX, posY, w, h) {
  this.x = posX || 0;
  this.y = posY || 0;
  this.width = w || 0;
  this.height = h || 0;
}

winClass.prototype.getX = function() {
    return(this.x);
}

winClass.prototype.setX = function(newX) {
    this.x = newX;
}

这篇关于Javascript动态的getter / setter为私有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆