【发布时间】:2020-01-27 19:41:06
【问题描述】:
所以我知道我可以像这样直接向函数添加公共属性。
const print = function (string) {
console.log(string);
};
print.uppercase = function (string) {
print(string.toUpperCase());
};
print("apple"); // apple
print.uppercase("apple"); // APPLE
但我在制作对象时总是遵循这种模式。
const object = function () {
let field;
const getField = function () {
return field;
};
const setField = function (_field) {
field = _field;
};
return Object.freeze({
getField,
setField
});
};
是否可以在坚持这种模式的同时制作具有公共属性的函数对象?不使用this?
const factory = function () {
const generic = function () {};
const specific = function () {};
return Object.freeze({
// DO SOMETHING HERE SO THAT
});
};
// factory() invokes the generic function, and
// factory.specific() invokes the specific function
【问题讨论】:
标签: javascript function object static-methods