tostring

Can Javascript get a function as text?

孤街浪徒 提交于 2019-11-27 13:37:13
问题 Can Javascript get a function as text? I'm thinking like the inverse of eval(). function derp() { a(); b(); c(); } alert(derp.asString()); The result would be something like "a(); b(); c();" Does it exist? 回答1: Updated to include caveats in the comments below from CMS, Tim Down, MooGoo: The closest thing available to what you're after is calling .toString() on a function to get the full function text, like this: function derp() { a(); b(); c(); } alert(derp.toString()); //"function derp() { a

toString override in C++ [duplicate]

烈酒焚心 提交于 2019-11-27 13:28:01
This question already has an answer here: C++ equivalent of Java's toString? 5 answers In Java, when a class overrides .toString() and you do System.out.println() it will use that. class MyObj { public String toString() { return "Hi"; } } ... x = new MyObj(); System.out.println(x); // prints Hi How can I accomplish that in C++, so that: Object x = new Object(); std::cout << *x << endl; Will output some meaningful string representation I chose for Object ? Erik std::ostream & operator<<(std::ostream & Str, Object const & v) { // print something from v to str, e.g: Str << v.getX(); return Str; }

关于对象的key为另一对象的情况

与世无争的帅哥 提交于 2019-11-27 12:12:31
问题描述:下面的代码中,c的值是多少? var a = {}; var b = {}; var c = {}; c[a] = "123"; c[b] = "456"; console.log(c) 分析: 我们都知道,对象的key不能是对象,而这儿的a和b都是对象,那么这样写c有值吗? 那我们就来打印看看就知道了: 结果就是这个样子了。那我们看到c的结构,不免就知道了,c的key值是“[Object Object]”。那这个“[Object Object]”是怎么来的呢? 这样我们就或许知道了“[Object Object]”可能时怎么来的了。但是真的就是调用了toString()吗?啥时候调用的??? 带着这个疑问,我们做一个操作: 结果表明,在a和b作为c的key时,调用了toString()方法,那么也就说得通了,对象不能作为另一对象的key值。这儿a和b自身调用了toString,然后才屈居于c的。 所以,最后的结果也就不难得出了,同一个对象,对同一个key作两次赋值,那么第二次自然就覆盖了第一次。 来源: https://blog.csdn.net/qq_41261490/article/details/99671288

What is the best standard style for a toString implementation? [closed]

人走茶凉 提交于 2019-11-27 11:57:04
问题 We have a lot of objects for which we like to implement a simple toString to output attributes of the object. Some of these attributes may be complex objects themselves. Is there any standard, or simply just a best practice for a style? I'm thinking something like: [SimpleClassName] { prop1:value, prop2:value } In which case a nested value would look like: [SimpleClassName] { prop1:value, prop2:[NestedObject] { prop3:value}} We are using Java but I find myself asking the same question in most

Why does the default Object.toString() return a hex representation of the hashCode?

与世无争的帅哥 提交于 2019-11-27 09:28:00
I'm curious why Object.toString() returns this: return getClass().getName() + "@" + Integer.toHexString(hashCode()); as opposed to this: return getClass().getName() + "@" + hashCode(); What benefits does displaying the hash code as a hex rather than a decimal buy you? Konrad Rudolph Object.hashCode used to be computed based on a memory location where the object is located . Memory locations are almost universally displayed as hexadecimal. The default return value of toString isn’t so much interested in the hash code but rather in a way to uniquely identify the object for the purpose of

WPF IsEditable=true ComboBox filled with objects displays the ToString() as the selected item

牧云@^-^@ 提交于 2019-11-27 08:54:27
The Wpf combo box allows editing, and this is fine if all your combo box items are strings, or have a ToString() method defined on them. When you select an item, it is displayed as Text, it does not use a DataTemplate, it just calls ToString() on the item that is selected. I get a list of items in my combo drop down that are formatted using my item template, when i select one i get the name of the object i.e. MyNamespace.MyObjectName Some solutions have been use ValuePath to bind to a property on the object, but if you require your display to be more than one of these, bad luck. implement the

Overriding toString method

≯℡__Kan透↙ 提交于 2019-11-27 08:30:18
问题 I am using .toString to return a string representation of an object, i.e. jcb.engineMove(move.toString()); will produce e2e4. What I am trying to do is to extract the text of this object (e2e4) as a string. After Googling I came across overriding the toString method so I came up with this: @Override public String toString() { String s = ""; int newRank = getRank(); int newFile = getFile(); final Move move = new Move(rank, file, newRank, newFile); s+="" + move; return s; } My questions are

Getting weird text back with EditText.toString() method in Android project. Why?

爱⌒轻易说出口 提交于 2019-11-27 07:27:16
问题 I appear to have a fundamental gap in my understanding of an EditText object. I have an Activity+Layout with a single EditText object. After I type a few characters into the EditText object and hit the Enter key, I retrieve the text in my onKey() listener. When I use the toString() method to retrieve the text I get back a weird string like: android.widget.EditText@43749ff0 Despite the fact the EditText.mText property does show the string I entered, "123" during my tests. Why is toString()

Explicit vs implicit call of toString

旧街凉风 提交于 2019-11-27 07:13:30
I used to use the implicit call of toString when wanting some debug info about an object, because in case of the object is null it does not throw an Exception. For instance: System.out.println("obj: "+obj); instead of: System.out.println("obj: "+obj.toString()); Is there any difference apart from the null case? Can the latter case work, when the former does not? Edit: What exactly is done, in case of the implicit call? There's little difference. Use the one that's shorter and works more often. If you actually want to get the string value of an object for other reasons, and want it to be null

Override valueof() and toString() in Java enum

烈酒焚心 提交于 2019-11-27 07:09:10
The values in my enum are words that need to have spaces in them, but enums can't have spaces in their values so it's all bunched up. I want to override toString() to add these spaces where I tell it to. I also want the enum to provide the correct enum when I use valueOf() on the same string that I added the spaces to. For example: public enum RandomEnum { StartHere, StopHere } Call toString() on RandomEnum whose value is StartHere returns string "Start Here" . Call valueof() on that same string ( "Start Here" ) returns enum value StartHere . How can I do this? You can try out this code. Since