【发布时间】:2013-12-26 11:47:16
【问题描述】:
我正在探索一些代码,我看到了将一个构造函数嵌入到另一个构造函数中多次使用的做法:
/**
* @constructor
*/
function contact_duplicates_manager(global_objects)
{
this.field_processors =
{
"Phone1Number": new phone_processor(),
"Phone2Number": new phone_processor(),
"Phone3Number": new phone_processor()
}
//...here some other code
/**
* @constructor
*/
function phone_processor()
{
//...here some other code
function clear_phone(phone)
{
//...here some code
}
this.is_equals = function (value1, value2)
{
return is_equals(clear_phone(value1), clear_phone(value2));
}
}
}
//... later in the code
var valid = this.field_processors[fld_name]["is_equals"](native_value, custom_value)
你认为phone_processor的构造函数应该在contact_duplicates_manager之外吗?
【问题讨论】:
-
一开始我会写成可读的格式(Java风格)。
-
phone_processor()到底是怎么用的? -
这只是
function中的function,它没有任何错误。在我看来,封装仅在父函数中使用的代码是一种很好的做法。 -
@nnnnnn,我已经更新了示例以演示它的使用方式。
标签: javascript