如何在面向对象的方法中将类似的方法和变量组合在一起? [英] How do I group similar methods and variables together in an object oriented approach?
问题描述
我正在寻找标准方法来做到这一点..
这篇文章这里探讨3种方式。 此处。你应该以哪种方式用于通用案例...这是一个偏好问题。
例如,如果我使用实例化方式..我有两行代码而不是一行...我必须实例化对象...而不是调用方法...
如果我使用静态类型实现而不是实例化任何对象。
模块模式的第3个选项......更昂贵b.c.它是自动执行的。
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:
- 对象 - 非静态
- 对象 - 静态
- 模块模式
一般情况下建议使用哪种方式,因此每次开始合并方法时我都不必讨论这个问题。
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;
}
推荐答案
这是我一般遵循的规则:
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 complex 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.net/dbrecht/WLN6Y/
这篇关于如何在面向对象的方法中将类似的方法和变量组合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!