在该对象的函数内迭代对象的所有成员

时间:2021-03-08 19:26:45

It would be exceedingly handy if I could do this:

如果我能做到这一点,那将非常方便:

var MyObject = function(param1, param2, ... paramN)
{
    this.var1 = stuff;
    this.var2 = moreStuff;
    .
    .
    .
    this.varN = nStuff;

    this.validate = function()
    {
        for(var current in this)
        {
            alert(current);
            //validate all member variables (even this function I suppose)
        }
    };
};

This however does not seem to do what I would want. I realize that the loop would eventually have to loop over it's parent function (which also, not surprisingly, does not happen).

然而,这似乎并不像我想要的那样。我意识到循环最终必须循环它的父函数(这也不奇怪,也不会发生)。

Is this impossible because the 'this' in the second function refers to the second function and not the first? Or is the keyword 'this' only a declaration operator for a public member and not a reference to the outer object ?

这是不可能的,因为第二个函数中的'this'指的是第二个函数而不是第一个函数?或者关键字“this”只是公共成员的声明运算符而不是对外部对象的引用?

I figure getting what I want this way is not possible but is there another way I can go about achieving this behaviour ?

我想通过这种方式获得我想要的东西是不可能的,但是我还有另一种方法可以实现这种行为吗?

2 个解决方案

#1


I think you're trying to get the value of the member and going about it the wrong way so try this:

我认为你试图获得会员的价值并以错误的方式去做,所以试试这个:

      var MyObject = function() {
        this.var1 = 'var 1 value';
        this.var2 = 'var 2 value';
        this.varN = 'var n value';
        var self = this;

        this.validate = function() {
          for (var member in self) {
            if (!self.hasOwnProperty(member) || typeof(self[member]) === "function") continue;
            alert(self[member]);
          }
        };
      };

      var m = new MyObject();
      m.validate();

To explain: the loop check first if the property is a user defined property as opposed to being inherited from the Object object. It also checks that the member is not a function (like validate()) it then alerts the value of the member.

解释:循环检查首先是属性是用户定义的属性,而不是从Object对象继承。它还检查该成员不是一个函数(如validate()),然后它会警告该成员的值。

The hasownproperty check is recommended by Douglas Crockford (father of JS) as best practice when iterating over memebers.

Douglas Crockford(JS的父亲)推荐使用hasproproperty检查作为迭代memebers时的最佳实践。

Hope this helps,

希望这可以帮助,

Darko

EDIT: Forgot to mention self - i included this because its the standard way of making sure that your this is actually what you want it to be.

编辑:忘了提自我 - 我把它包括在内,因为它是确保你实际上是你想要它的标准方法。

#2


How are you calling validate?

你怎么称呼验证?

The following code works fine for me:

以下代码对我来说很好:

var MyObject = function(){
    this.var1 = 'stuff';
    this.var2 = 'moreStuff';
    this.varN = 'Stuff';

    this.validate = function()
    {
        for(var current in this)
        {
            alert(current);
        }
    };
};

var m = new MyObject();
m.validate();

#1


I think you're trying to get the value of the member and going about it the wrong way so try this:

我认为你试图获得会员的价值并以错误的方式去做,所以试试这个:

      var MyObject = function() {
        this.var1 = 'var 1 value';
        this.var2 = 'var 2 value';
        this.varN = 'var n value';
        var self = this;

        this.validate = function() {
          for (var member in self) {
            if (!self.hasOwnProperty(member) || typeof(self[member]) === "function") continue;
            alert(self[member]);
          }
        };
      };

      var m = new MyObject();
      m.validate();

To explain: the loop check first if the property is a user defined property as opposed to being inherited from the Object object. It also checks that the member is not a function (like validate()) it then alerts the value of the member.

解释:循环检查首先是属性是用户定义的属性,而不是从Object对象继承。它还检查该成员不是一个函数(如validate()),然后它会警告该成员的值。

The hasownproperty check is recommended by Douglas Crockford (father of JS) as best practice when iterating over memebers.

Douglas Crockford(JS的父亲)推荐使用hasproproperty检查作为迭代memebers时的最佳实践。

Hope this helps,

希望这可以帮助,

Darko

EDIT: Forgot to mention self - i included this because its the standard way of making sure that your this is actually what you want it to be.

编辑:忘了提自我 - 我把它包括在内,因为它是确保你实际上是你想要它的标准方法。

#2


How are you calling validate?

你怎么称呼验证?

The following code works fine for me:

以下代码对我来说很好:

var MyObject = function(){
    this.var1 = 'stuff';
    this.var2 = 'moreStuff';
    this.varN = 'Stuff';

    this.validate = function()
    {
        for(var current in this)
        {
            alert(current);
        }
    };
};

var m = new MyObject();
m.validate();