programing

[객체 객체]는 무엇을 의미합니까?

nasanasas 2020. 9. 9. 08:07
반응형

[객체 객체]는 무엇을 의미합니까?


함수에서 반환 된 값을 경고하려고하는데이 경고가 나타납니다.

[객체 객체]

다음은 자바 스크립트 코드입니다.

<script type="text/javascript">
$(function ()
{
var $main = $('#main'),
    $1 = $('#1'),
    $2 = $('#2');

$2.hide(); // hide div#2 when the page is loaded

$main.click(function ()
{
    $1.toggle();
    $2.toggle();
});

 $('#senddvd').click(function ()
{
   alert('hello');
   var a=whichIsVisible();
   alert(whichIsVisible());
});


function whichIsVisible()
{
    if (!$1.is(':hidden')) return $1;
    if (!$2.is(':hidden')) return $2;
}

 });

 </script>

whichIsVisible은 내가 확인하려는 기능입니다.


객체에서 문자열로의 기본 변환은 "[object Object]"입니다.

jQuery 객체를 처리 할 때 다음을 수행 할 수 있습니다.

alert(whichIsVisible()[0].id);

요소의 ID를 인쇄합니다.

주석에서 언급했듯이 Firefox 또는 Chrome과 같은 브라우저에 포함 된 도구를 console.log(whichIsVisible())사용하여 alert.

참고 : ID는 숫자로 시작하면 안됩니다.


다른 사람들이 언급했듯이 이것은 객체의 기본 직렬화입니다. 하지만 왜 [object Object]그럴까요 [object]?

Javascript에는 다양한 유형의 객체가 있기 때문입니다!

  • 함수 개체 :
    stringify(function (){})->[object Function]
  • 배열 객체 :
    stringify([])->[object Array]
  • RegExp 객체
    stringify(/x/) ->[object RegExp]
  • 날짜 개체
    stringify(new Date) ->[object Date]
  • 몇 가지 더
  • 개체 개체 !
    stringify({})->[object Object]

생성자 함수가 호출되고 Object(대문자 "O"로) "객체"(작은 "o"로)라는 용어는 사물의 구조적 특성을 나타 내기 때문입니다.

일반적으로 자바 스크립트에서 "객체"에 대해 이야기 할 때 실제로 는 다른 유형이 아니라 " 객체 객체 "를 의미 합니다.

여기서 stringify다음과 같아야합니다 :

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


[object Object] javascript에서 객체의 기본 toString 표현입니다.

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

for(var property in obj) {
    alert(property + "=" + obj[property]);
}

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.


It's the value returned by that object's toString() function.


I understand what you're trying to do, 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 do 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 to debug a problem. If you're using Firefox, Firebug is excellent. If you're using IE8, Safari, or Chrome, they have built-in debuggers.


You can see value inside [object Object] like this

Alert.alert(  JSON.stringify(userDate)  );

Try like this

    realm.write(() => {
       const userFormData = realm.create('User',{
       user_email: value.username,
       user_password: value.password,
      });
     });

      const userDate = realm.objects('User').filtered('user_email == $0', value.username.toString(), );
      Alert.alert(  JSON.stringify(userDate)  );

reference

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


[object Object] is the default string representation of a JavaScript Object. It is what you'll get if you run this code:

alert({}); // [object Object]

You can change the default representation by overriding the toString method like so:

var o = {toString: function(){ return "foo" }};
alert(o); // foo

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 and coercion.

string, number, boolean, null, undefined, and symbol.

In JavaScript there are 7 primitive types: undefined, null, boolean, string, number, bigint and 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 and objects. In this case the string value is coerced to a string object in order to access the property length. The string object is only used for a fraction of second after which it is sacrificed to the Gods of garbage collection – but in the spirit of the TV discovery shows, we will trap the elusive creature and preserve it for further analysis…

To demonstrate this further consider the following example in which we are adding a new property to String constructor prototype.

String.prototype.sampleProperty = 5;
var str = "this is a string";
str.sampleProperty;            // 5

By this means primitives have access to all the properties (including methods) defined by their respective object constructors.

So we saw that primitive types will appropriately coerce to 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();     // "[object Object]"
myFunc.toString();    // "function(){}"
myString.toString();  // "This is a sample String"
myNumber.toString();  // "4"
myArray.toString();   // "2,3,5"

As discussed above, what's really happening is when we call toString() method on a primitive type, it has to be coerced into its object counterpart before it can invoke the method.
i.e. myNumber.toString() is equivalent to Number.prototype.toString.call(myNumber) and similarly for other primitive types.

But what if instead of primitive type being passed into toString() method of its corresponding Object constructor function counterpart, we force the primitive type to be passed as parameter onto 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:

  1. If the this value is undefined, return "[object Undefined]".
  2. If the this value is null, return "[object Null]".
  3. If this value is none of the above, Let O be the result of calling toObject passing the this value as the argument.
  4. Let class be the value of the [[Class]] internal property of O.
  5. Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".

Understand this from the following example

var myObj       = {lhs: 3, rhs: 2};
var myFunc      = function(){}
var myString    = "This is a sample String";
var myNumber    = 4;
var myArray     = [2, 3, 5];
var myUndefined = undefined;
var myNull      = null;

Object.prototype.toString.call(myObj);        // "[object Object]"
Object.prototype.toString.call(myFunc);       // "[object Function]"
Object.prototype.toString.call(myString);     // "[object String]"
Object.prototype.toString.call(myNumber);     // "[object Number]"
Object.prototype.toString.call(myArray);      // "[object Array]"
Object.prototype.toString.call(myUndefined);  // "[object Undefined]"
Object.prototype.toString.call(myNull);       // "[object Null]"

References: https://es5.github.io/x15.2.html#x15.2.4.2 https://es5.github.io/x9.html#x9.9 https://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/


You have a javascript object

$1 and $2 are jquery objects, maybe use alert($1.text()); to get text or alert($1.attr('id'); etc...

you have to treat $1 and $2 like jQuery objects.


You are trying to return an object. Because there is no good way to represent an object as a string, the object's .toString() value is automatically set as "[object Object]".

참고URL : https://stackoverflow.com/questions/4750225/what-does-object-object-mean

반응형