【发布时间】:2014-09-03 19:12:49
【问题描述】:
我正在定义一个类:
MyClass = function () {
// Class member variables here
};
MyClass.prototype.MyFunction = function () {
// Do stuff, see below
};
我不确定的是 MyFunction。这是我目前的模式(函数中的函数)。我有这种方式是因为它看起来很整洁。 MyFunctionSubFunction 仅与 MyFunction 相关联,因此从样式的角度来看,我假设 MyFunctionSubFunction 应该在 MyFunction 的定义范围内。
MyClass.prototype.MyFunction = function () {
var i, j, iIter, jIter, a, b, c, val;
var MyFunctionSubFunction = function (a, b, c) {
// Do things with a, b and c
};
// iIter and jIter are set to values depending on what is going on
for(i=0; i<iIter; i++) {
for(j=0; j<jIter; j++) {
// a, b and c are now set depending on i and j
MyFunctionSubFunction(a, b, c);
}
}
};
这是一种好的编码习惯(函数中的函数)吗?
这是否针对速度和其他方面进行了优化?
MyFunction(上层函数)每秒被调用大约 250 次(它是一个游戏,这是 AI 代码的一部分)。
或者我应该这样做吗?:
MyClass.prototype.MyFunction = function () {
var i, j, iIter, jIter, a, b, c, val;
// iIter and jIter are set to values depending on what is going on
for(i=0; i<iIter; i++) {
for(j=0; j<jIter; j++) {
// a, b and c are now set depending on i and j
this.MyFunctionSubFunction(a, b, c);
}
}
};
MyClass.prototype.MyFunctionSubFunction = function (a, b, c) {
// Do things with a, b and c
};
【问题讨论】:
标签: javascript function class optimization