我正在阅读JSVerbalExpression源代码,我试图理解作者为什么在构造函数的原型中注入方法,而不仅仅是分配它们:
function VerbalExpression() {
var verbalExpression = new RegExp();
// Add all the class methods
VerbalExpression.injectClassMethods(verbalExpression);
// Return the new object.
return verbalExpression;
}
/**
* @param {RegExp} verbalExpression An instance of RegExp on which to add VerbalExpressions methods
* @return {RegExp} A new instance of RegExp with injected methods
*/
VerbalExpression.injectClassMethods = function injectClassMethods(verbalExpression) {
var method;
// Loop over all the prototype methods
for (method in VerbalExpression.prototype) {
// Make sure this is a local method.
if (VerbalExpression.prototype.hasOwnProperty(method)) {
// Add the method
verbalExpression[method] = VerbalExpression.prototype[method];
}
}
return verbalExpression;
};
它与性能有关吗?
感谢。