简体   繁体   English

如何在javascript中实现此逻辑?

[英]How to implement this logic in javascript?

I am using javascript in my project and i am a newbie. 我在我的项目中使用javascript,我是新手。 I have a scenario where i want to implement a Status property for my component. 我有一种情况,我想为我的组件实现Status属性。 This Status property show the current status of my component out of a list of available status. 此状态属性显示可用状态列表中我组件的当前状态。 For example: 例如:

I implemented in this way: 我以这种方式实现:

  var availableStatus = [
                          { Name: 'visible' }, 
                          { Name: 'hidden' }, 
                          { Name: 'deny' }, 
                          { Name: 'allow' }, 
                          { Name: 'destroy' }
                         ];

  var Status = availableStatus[0];

  if(Status === availableStatus[0])
    //do this 
  else if(Status === availableStatus[1])
    //do this 
  ...

But i feel that there should be some another better approach for this. 但是我认为应该为此提供另一种更好的方法。 And this logic is dependent on array items order which may create problem in future if array order changes. 而且此逻辑取决于数组项的顺序,如果数组顺序发生更改,将来可能会产生问题。 I have another logic for implement this by using Named Array. 我还有另一个使用命名数组来实现此目的的逻辑。 But i dont know how. 但是我不知道如何。

Please can anyone give me a better suggestion to implement this kind of scenario ?? 请谁能给我一个更好的建议来实施这种方案?

Index them by their name (if that is sufficiently unique): 通过名称对其进行索引(如果该名称足够唯一):

var availableStatus = {
                      visible : { Name: 'visible' }, 
                      hidden : { Name: 'hidden' }, 
                      deny : { Name: 'deny' }, 
                      allow : { Name: 'allow' }, 
                      destroy : { Name: 'destroy' }
                     };

var Status = availableStatus.visible;

if(Status === availableStatus.visible)
    //do this 
else if(Status === availableStatus.hidden)
    //do this 

This is assuming that there is some more information in the status objects later on, or functions working on that data. 这是假定稍后状态对象或对该数据起作用的功能中还有更多信息。 If you just want to store a string you can just do it like this: 如果您只想存储一个字符串,则可以这样操作:

var availableStatus = {
                      visible : {},
                      hidden : {}, 
                      deny : {}, 
                      allow : {}, 
                      destroy : {}
                     };

and have the same functionality. 并具有相同的功能。

It sounds like you want to use a switch statement: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch . 听起来您想使用switch语句: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/switch

switch(Status) {
  case 'visible':
    // do something...
    break;
  case 'hidden':
    // do something else...
  // etc.
}

How about that? 那个怎么样? You can store more information in the dictionary assigned to the status, eg "can_recieve_messages". 您可以在分配给状态的字典中存储更多信息,例如“ can_recieve_messages”。

    var availableStatus = {
                          'visible': { id: 0 }, 
                          'hidden':  { id: 1},
                          'deny' : { id: 2} [...]
                         };

    var Status = availableStatus.visible;

    switch (Status ) {
        case availableStatus.visible:
           // on visible
           break;
        case availableStatus.hidden:
           // on hidden
           break;
      [...]
    }
 var availableStatus ={};

                        availableStatus.['visible']='visible';
                        availableStatus.['hidden']= 'hidden';
                        availableStatus.['deny']= 'deny';
                        availableStatus.['deny']= 'deny' ; 
                        availableStatus.['destroy']= 'destroy';

i have st key name as same as value you can use any name for key and value 我具有与值相同的st键名,您可以使用任何名称作为键和值

   availableStatus.['test']= 'deny' ; 

and you can use anything without the fear of order if you write availableStatus.destroy then out put will destroy 如果您编写了availableStatus.destroy,那么您可以使用任何东西而不必担心顺序,那么输出将破坏

and availableStatus.test then output will be deny availableStatus.test然后输出将被拒绝

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM