Why {} + {} gives "

I am trying to lớn alert a returned value from a function và I get this in the alert:

Here is the JavaScript code:

whichIsVisible is the function which I am trying lớn check on.

Bạn đang xem: Why {} + {} gives "


*

*

It means the datatype of what you're returning is an Object. –user1385191 Jan đôi mươi "11 at 17:08
The mặc định conversion from an object to string is "".

As you are dealing with jQuery objects, you might want to do

alert(whichIsVisible()<0>.id);khổng lồ print the element"s ID.

As mentioned in the comments, you should use the tools included in browsers lượt thích Firefox or Chrome khổng lồ introspect objects by doing console.log(whichIsVisible()) instead of alert.

Sidenote: IDs should not start with digits.


*

*

As others have sầu noted, this is the default serialisation of an object. But why is it and not just ?

That is because there are different types of objects in Javascript!

Function objects:stringify(function ()) -> Array objects:stringify(<>) -> RegExp objectsstringify(/x/) -> Date objectsstringify(new Date) -> and Object objects!stringify() ->

That"s because the constructor function is called Object (with a capital "O"), and the term "object" (with small "o") refers khổng lồ the structural nature of the thingy.

Usually, when you"re talking about "objects" in Javascript, you actually mean "Object objects", & not the other types.

where stringify should look like this:

function stringify (x) console.log(Object.prototype.toString.call(x));


Share
Improve this answer
Follow
edited Jun 5 "18 at 11:34
*

Muhammad Shaharyar
90666 silver badges2222 bronze badges
answered Aug 21 "14 at 6:25
user123444555621user123444555621
131k2525 gold badges104104 silver badges122122 bronze badges
5
Add a comment |
26
is the default toString representation of an object in javascript.

If you want to know the properties of your object, just foreach over it like this:

for(var property in obj) alert(property + "=" + obj);In your particular case, you are getting a jQuery object. Try doing this instead:

$("#senddvd").click(function () alert("hello"); var a=whichIsVisible(); alert(whichIsVisible().attr("id")););This should alert the id of the visible element.

Xem thêm: Nghĩa Của Từ Reply On Là Gì ? Reply Là Gì, Nghĩa Của Từ Reply


Share
Improve sầu this answer
Follow
answered Jan đôi mươi "11 at 17:09
alexnalexn
53.5k1414 gold badges107107 silver badges142142 bronze badges
1
Add a bình luận |
17
You can see value inside lượt thích this

Alert.alert( JSON.stringify(userDate) );Try like this

https://off.tokyo/blog/react-native-object-object/


Share
Improve sầu this answer
Follow
answered Aug 1 "18 at 16:32
Ryosuke HujisawaRyosuke Hujisawa
1,7831111 silver badges1414 bronze badges
1
Add a comment |
11
It"s the value returned by that object"s toString() function.

I understand what you"re trying lớn bởi vì, because I answered your question yesterday about determining which div is visible. :)The whichIsVisible() function returns an actual jQuery object, because I thought that would be more programmatically useful. If you want to use this function for debugging purposes, you can just bởi vì something like this:

function whichIsVisible_v2() if (!$1.is(":hidden")) return "#1"; if (!$2.is(":hidden")) return "#2";That said, you really should be using a proper debugger rather than alert() if you"re trying lớn debug a problem. If you"re using Firefox, Firebug is excellent. If you"re using IE8, Safari, or Chrome, they have built-in debuggers.


Share
Improve this answer
Follow
edited May 23 "17 at 12:02
Community♦
111 silver badge
answered Jan đôi mươi "11 at 17:09
Matt BallMatt Ball
333k9292 gold badges617617 silver badges685685 bronze badges
1
Add a comment |
10
Basics

You may not know it but, in JavaScript, whenever we interact with string, number or boolean primitives we enter a hidden world of object shadows và coercion.

string, number, boolean, null, undefined, và symbol.

In JavaScript there are 7 primitive types: undefined, null, boolean, string, number, bigint và symbol. Everything else is an object. The primitive types boolean, string and number can be wrapped by their object counterparts. These objects are instances of the Boolean, String and Number constructors respectively.

typeof true; //"boolean"typeof new Boolean(true); //"object"typeof "this is a string"; //"string"typeof new String("this is a string"); //"object"typeof 123; //"number"typeof new Number(123); //"object"If primitives have no properties, why does "this is a string".length return a value?

Because JavaScript will readily coerce between primitives và objects. In this case the string value is coerced to a string object in order to lớn access the property length. The string object is only used for a fraction of second after which it is sacrificed khổng lồ the Gods of garbage collection – but in the spirit of the TV discovery shows, we will trap the elusive creature and preserve sầu it for further analysis…

To demonstrate this further consider the following example in which we are adding a new property khổng lồ String constructor prototype.

String.prototype.sampleProperty = 5;var str = "this is a string";str.sampleProperty; // 5By this means primitives have access lớn all the properties (including methods) defined by their respective sầu object constructors.

Xem thêm: Hàm Tìm Số Trùng Nhau Trong Excel 2003, 2007, 2010,2013, 2016

So we saw that primitive sầu types will appropriately coerce to lớn their respective Object counterpart when required.

Analysis of toString() method

Consider the following code

var myObj = lhs: 3, rhs: 2;var myFunc = function()var myString = "This is a sample String";var myNumber = 4;var myArray = <2, 3, 5>;myObj.toString(); // ""myFunc.toString(); // "function()"myString.toString(); // "This is a sample String"myNumber.toString(); // "4"myArray.toString(); // "2,3,5"As discussed above sầu, what"s really happening is when we Call toString() method on a primitive type, it has to be coerced inkhổng lồ its object counterpart before it can invoke the method.i.e. myNumber.toString() is equivalent to Number.prototype.toString.call(myNumber) & similarly for other primitive types.

But what if instead of primitive type being passed into lớn toString() method of its corresponding Object constructor function counterpart, we force the primitive sầu type lớn be passed as parameter onkhổng lồ toString() method of Object function constructor (Object.prototype.toString.call(x))?

Closer look at Object.prototype.toString()

As per the documentation, When the toString method is called, the following steps are taken:

If the this value is undefined, return "". If the this value is null, return "". Let class be the value of the <> internal property of O. Return the String value that is the result of concatenating the three Strings "".

Underst& this from the following example