javascript - How do I group similar methods and variables together in an object oriented approach? - Stack Overflow

I'm looking for standard way to do this..This post here explores 3 ways. Here to.Which way shou

I'm looking for standard way to do this.. This post here explores 3 ways. Here to. Which way should you use for a generic case...is it a matter of preference.
For example if I use the instantiation way..than I have two lines of code instead of one...I have to instanitate the object...and than call the method... If I use a static type implementation than I don't have to instantiate any objects. The 3rd option the module pattern...is more costly b.c. it is self-executing.

If I had to name them:

  1. object - non static
  2. object - static
  3. module pattern

Which way is suggested in general so I don't have to debate this each time I begin to consolidate methods.

Here is an example of collected methods I would like to consolidate.

/********************
group:checks
********************/

var patterns = 
  {
  name: /^[a-zA-Z-\s]{1,20}$/,
  email: /^[a-zA-Z0-9._(-)]+@[a-zA-Z0-9.(-)]+\.[a-zA-Z]{1,4}$/,
  pass: /.{6,40}/,
  url:  /^[(-)\w&:\/\.=\?,#+]{1,}$/,
  aml:  /<(.+)_([a-z]){1}>$/
  };

/*
- check_generic() - generic 
*/

function check_generic(reg_ex_in,text,html_id,response)
  {
  if(!reg_ex_in.exec(text.value))
    {
    fill_id(html_id,response);
    return 0;
    }
  return 1;
  }

/*
- check_empty() - checks for empty text 
*/

function check_empty(text,html_id,response) 
  {
  for(var a=0;a<text.length;a++)
    {
    if(text[a].value==='')
      {
      fill_id(html_id,response);
      return 0;
      }
    }
  return 1;
  }

/*
- check_same() - checks if two text entries are the same 
*/

function check_same(text1,text2,html_id,response)
  {
  if((text1.value)!==(text2.value))
    {
    fill_id(html_id,response);return 0;
    }
  fill_id(html_id,'');
  return 1;
  }

I'm looking for standard way to do this.. This post here explores 3 ways. Here to. Which way should you use for a generic case...is it a matter of preference.
For example if I use the instantiation way..than I have two lines of code instead of one...I have to instanitate the object...and than call the method... If I use a static type implementation than I don't have to instantiate any objects. The 3rd option the module pattern...is more costly b.c. it is self-executing.

If I had to name them:

  1. object - non static
  2. object - static
  3. module pattern

Which way is suggested in general so I don't have to debate this each time I begin to consolidate methods.

Here is an example of collected methods I would like to consolidate.

/********************
group:checks
********************/

var patterns = 
  {
  name: /^[a-zA-Z-\s]{1,20}$/,
  email: /^[a-zA-Z0-9._(-)]+@[a-zA-Z0-9.(-)]+\.[a-zA-Z]{1,4}$/,
  pass: /.{6,40}/,
  url:  /^[(-)\w&:\/\.=\?,#+]{1,}$/,
  aml:  /<(.+)_([a-z]){1}>$/
  };

/*
- check_generic() - generic 
*/

function check_generic(reg_ex_in,text,html_id,response)
  {
  if(!reg_ex_in.exec(text.value))
    {
    fill_id(html_id,response);
    return 0;
    }
  return 1;
  }

/*
- check_empty() - checks for empty text 
*/

function check_empty(text,html_id,response) 
  {
  for(var a=0;a<text.length;a++)
    {
    if(text[a].value==='')
      {
      fill_id(html_id,response);
      return 0;
      }
    }
  return 1;
  }

/*
- check_same() - checks if two text entries are the same 
*/

function check_same(text1,text2,html_id,response)
  {
  if((text1.value)!==(text2.value))
    {
    fill_id(html_id,response);return 0;
    }
  fill_id(html_id,'');
  return 1;
  }
Share Improve this question edited May 23, 2017 at 12:03 CommunityBot 11 silver badge asked Nov 15, 2011 at 0:55 user656925user656925 3
  • The thing is, each way has different characteristics and is better suited for different situations and types of program. You really just need to know all the alternatives and choose appropriately depending on the situation. – hugomg Commented Nov 15, 2011 at 1:05
  • differnet shoes...depending upon what you are doing...running....gong to work...etc...but say you get only one shoe... – user656925 Commented Nov 15, 2011 at 1:32
  • Except that the shoes are free and you can have as many as you want. – hugomg Commented Nov 15, 2011 at 2:17
Add a ment  | 

1 Answer 1

Reset to default 6

Here's the rule I generally follow:

If I'm grouping a set of stateless static functions together (essentially namespacing them), I'll stick them in an object:

var MyNamespace = {
   patterns: {
      name: /^[a-zA-Z-\s]{1,20}$/,
      email: /^[a-zA-Z0-9._(-)]+@[a-zA-Z0-9.(-)]+\.[a-zA-Z]{1,4}$/,
      pass: /.{6,40}/,
      url:  /^[(-)\w&:\/\.=\?,#+]{1,}$/,
      aml:  /<(.+)_([a-z]){1}>$/
   },

   check_generic: function(reg_ex_in,text,html_id,response)
   {
      if(!reg_ex_in.exec(text.value))
      {
        fill_id(html_id,response);
        return 0;
      }
      return 1;
   }
};

If I want to have a stateful class implementation without concerning myself about access control, I'll use the instantiation method:

var MyClass = function(param) {
    // initialize here
};
MyClass.prototype = {
   // method definitions here
};

If I'm doing something a little more plex that requires access control, I'll use the module pattern:

var mod = (function() {
    var o = {};
    var _p = "myprivatestring";
    o.p = "mypublicstring";

    o.myfunc = function() {
        this.p = _p;
    };

    return o;
})();

console.log(mod.p);
mod.myfunc();
console.log(mod.p);

http://jsfiddle/dbrecht/WLN6Y/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744179459a4561912.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信