Getter 在 javascript 函数本身的实例上启用文档字符串。可能吗?
Getter on the instance of a javascript function itself to enable docstrings. Is it possible?
乌托邦目标:
str = "hello"
str.doc = "this is a string"
console.log(str) //prints 'hello'
console.log(str.doc) //should prints the docstring
但是,字符串是不可变的,上述情况是不可能的。
我如何尝试解决方法:
str = new String("hello")
str.doc = "this is a string"
console.log(str) //prints the string object but not 'hello'
console.log(""+str) //prints "hello"
console.log(str.doc) //correctly prints the docstring
如您所见,只有在调用 .valueOf() 或 .toString() 时,您才能获得原始字符串。目标是直接调用时获取字符串。
另一种变通方法(在 coffescript 中,为了简短起见:3 行 vs ~15 行)
class Configuration
constructor: (@value, @doc) ->
@::valueOf = -> @value
这与 new String()
非常相似。它只是对象上 valueOf()
方法的原型。问题和上面一样。您不能直接 console.log(str)
打印原始字符串并且都使用 console.log(str.doc)
问题:
是否可以通过某种方式实现这一点,而不必重构我的整个代码库?
我不能容忍这种做法,但您可以覆盖 console.log
,以便它在任何字符串参数上调用 valueOf:
var oldConsoleLog = console.log;
console.log = function ( ) {
oldConsoleLog.apply( console, [].map.call( arguments, function ( arg ) { return arg instanceof String ? arg.valueOf() : arg; } ) );
};
您可以对字符串进行原型设计。参见 fiddle
// String.prototype.__defineGetter__("doc", function() { return "test" }); //deprecated
Object.defineProperty(String.prototype, "doc", {
get: function doc() {
return "this is a string";
}
});
console.log("this is a string".doc);
如评论中所述,我将进行一些重构,用 str + ""
替换 str
等变量,然后使用 new String()
,不是很干净,但似乎是唯一的快速解决方法。事实上,最佳解决方案是简单地使用如下所示的单例:
config = {
mySetting: {
value: "settingValue",
docstring: "my doc string"
}
}
并在必要时使用它:config.mySetting.value
但这将是对一个复杂的应用程序进行大量重构,这不是现在的优先事项。谢谢大家的回复!
乌托邦目标:
str = "hello"
str.doc = "this is a string"
console.log(str) //prints 'hello'
console.log(str.doc) //should prints the docstring
但是,字符串是不可变的,上述情况是不可能的。
我如何尝试解决方法:
str = new String("hello")
str.doc = "this is a string"
console.log(str) //prints the string object but not 'hello'
console.log(""+str) //prints "hello"
console.log(str.doc) //correctly prints the docstring
如您所见,只有在调用 .valueOf() 或 .toString() 时,您才能获得原始字符串。目标是直接调用时获取字符串。
另一种变通方法(在 coffescript 中,为了简短起见:3 行 vs ~15 行)
class Configuration
constructor: (@value, @doc) ->
@::valueOf = -> @value
这与 new String()
非常相似。它只是对象上 valueOf()
方法的原型。问题和上面一样。您不能直接 console.log(str)
打印原始字符串并且都使用 console.log(str.doc)
问题:
是否可以通过某种方式实现这一点,而不必重构我的整个代码库?
我不能容忍这种做法,但您可以覆盖 console.log
,以便它在任何字符串参数上调用 valueOf:
var oldConsoleLog = console.log;
console.log = function ( ) {
oldConsoleLog.apply( console, [].map.call( arguments, function ( arg ) { return arg instanceof String ? arg.valueOf() : arg; } ) );
};
您可以对字符串进行原型设计。参见 fiddle
// String.prototype.__defineGetter__("doc", function() { return "test" }); //deprecated
Object.defineProperty(String.prototype, "doc", {
get: function doc() {
return "this is a string";
}
});
console.log("this is a string".doc);
如评论中所述,我将进行一些重构,用 str + ""
替换 str
等变量,然后使用 new String()
,不是很干净,但似乎是唯一的快速解决方法。事实上,最佳解决方案是简单地使用如下所示的单例:
config = {
mySetting: {
value: "settingValue",
docstring: "my doc string"
}
}
并在必要时使用它:config.mySetting.value
但这将是对一个复杂的应用程序进行大量重构,这不是现在的优先事项。谢谢大家的回复!