admin管理员组文章数量:1026989
I often use function meta properties, i.e. like this
var func = function(){}
func.meta = "meta";
console.log(func);//output: function func()
console.log(func.meta);//output: "meta"
But when I tried using strings in similar way, it did't work.
var string = "string";
string.meta = "meta";
console.log(string);//output: "string"
console.log(string.meta);//output: undefined
Why and how to fix that?
I often use function meta properties, i.e. like this
var func = function(){}
func.meta = "meta";
console.log(func);//output: function func()
console.log(func.meta);//output: "meta"
But when I tried using strings in similar way, it did't work.
var string = "string";
string.meta = "meta";
console.log(string);//output: "string"
console.log(string.meta);//output: undefined
Why and how to fix that?
Share Improve this question asked Aug 27, 2015 at 15:50 setecsetec 16.2k3 gold badges38 silver badges51 bronze badges1 Answer
Reset to default 7Your string is a primitive value. It accepts the property because of some magic which temporarily converts it to an object (or you can think of it that way). So the assignment isn't useful after that.
You could use the new String
constructor to create an object you can actually hold on to, but that's very unusual for JavaScript.
var string = new String("string");
string.meta = "meta";
console.log(string);
console.log(string.meta);//output: "meta"
The console output on the string
itself will actually probably not show what you want, but in practice it should work in concatenation as such.
Keep in mind that typeof
will no longer give you "string"
as a result. It will now be "object"
.
Personally, I'd just create a custom constructor if you need to store more than the string itself and then use it with the knowledge that the object is posed of your string and the meta data.
I often use function meta properties, i.e. like this
var func = function(){}
func.meta = "meta";
console.log(func);//output: function func()
console.log(func.meta);//output: "meta"
But when I tried using strings in similar way, it did't work.
var string = "string";
string.meta = "meta";
console.log(string);//output: "string"
console.log(string.meta);//output: undefined
Why and how to fix that?
I often use function meta properties, i.e. like this
var func = function(){}
func.meta = "meta";
console.log(func);//output: function func()
console.log(func.meta);//output: "meta"
But when I tried using strings in similar way, it did't work.
var string = "string";
string.meta = "meta";
console.log(string);//output: "string"
console.log(string.meta);//output: undefined
Why and how to fix that?
Share Improve this question asked Aug 27, 2015 at 15:50 setecsetec 16.2k3 gold badges38 silver badges51 bronze badges1 Answer
Reset to default 7Your string is a primitive value. It accepts the property because of some magic which temporarily converts it to an object (or you can think of it that way). So the assignment isn't useful after that.
You could use the new String
constructor to create an object you can actually hold on to, but that's very unusual for JavaScript.
var string = new String("string");
string.meta = "meta";
console.log(string);
console.log(string.meta);//output: "meta"
The console output on the string
itself will actually probably not show what you want, but in practice it should work in concatenation as such.
Keep in mind that typeof
will no longer give you "string"
as a result. It will now be "object"
.
Personally, I'd just create a custom constructor if you need to store more than the string itself and then use it with the knowledge that the object is posed of your string and the meta data.
本文标签: javascriptAdd custom property to stringStack Overflow
版权声明:本文标题:javascript - Add custom property to string - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659963a2161828.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论