the murmurous sea

JS: String() vs. toString() and valueOf() 본문

#dev/개념정리

JS: String() vs. toString() and valueOf()

lunacer 2020. 5. 13. 19:13

The String() function returns the same value as toString() of the individual objects.
But
String() : can use it on values that are undefined or null.
toString() : will produce a TypeError exception.
String will often call an object's toString() anyway, so its a safer way of doing that.

String()


1. Constructor
2. Standard built-in objects

3. to create a new String object.
4. It performs type conversion when called as a function rather than as a constructor.
    : String function and String constructor produce different results.
    4-a. function
       : converts the value of an object to a string.

       : produces a string (the primitive type) as promised.

typeof String('Hello world'); // string

    4-b. constructor

typeof new String('Hello world'); // object

 

   : the constructor produces an instance of the type String (an object wrapper)

   : and that's why rarely use the String constructor at all.

 

[Syntax]

new String(thing)
String(thing)
thing required
A JavaScript object
Anything to be converted to a string.


@

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String

https://www.w3schools.com/Jsref/jsref_string.asp

 


Object.prototype.toString()

 

1. method 
2. returns a string representing the object.
3. Every object has a toString() method.
    By default, thetoString() method is inherited by every object descended from Object.

    : automatically called
      - when the object is to be represented as a text value
      - when an object is referred to in a manner in which a string is expected

4. Using toString() to detect object class
    : with every object, allows you to get its class.
    : need to call Function.prototype.call() or Function.prototype.apply() on it, passing the object you want to inspect as the first parameter (called thisArg).

const toString = Object.prototype.toString;

toString.call(new Date);    // [object Date]
toString.call(new String);  // [object String]
toString.call(Math);        // [object Math]

// Since JavaScript 1.8.5
toString.call(undefined);   // [object Undefined]
toString.call(null);        // [object Null]

 

5. By using radix, you can also convert base 10 numbers to another base numbers.
    : For Numbers and BigInts toString() takes an optional parameter
    : Radix the value of radix must be minimum 2 and maximum 36.
    : in an example,

더보기

converting base 10 number to a base 2 (binary)  number :

let baseTenInt = 10;
console.log(baseTenInt.toString(2)); 
// Expected output is "1010"
let bigNum = BigInt(20); 
console.log(bigNum.toString(2));
// Expected output is "10100"

 

[Syntax]

obj.toString()

 

[Return Value]

: A string representing the object.
: Returns "[object type]", where type is the object type.

const o = new Object();
o.toString(); // returns [object Object]

: Overriding the default toString method

function Dog(name, breed, color, sex) {
  this.name = name;
  this.breed = breed;
  this.color = color;
  this.sex = sex;
}

theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female');

//If you call the toString() method on this custom object, it returns the default value inherited from Object:
theDog.toString(); // returns [object Object]

//creates and assigns dogToString() to override the default toString() method.
Dog.prototype.toString = function dogToString() {
  const ret = 'Dog ' + this.name + ' is a ' + this.sex + ' ' + this.color + ' ' + this.breed;
  return ret;
}

//OR
//using ES6 template strings
Dog.prototype.toString = function dogToString() {
  return `Dog ${this.name} is a ${this.sex} ${this.color} ${this.breed}`;
}

//Return
//"Dog Gabby is a female chocolate Lab"

    : create a function to be called in place of the default toString() method. 
    : With the preceding code in place, any time theDog is used in a string context, JavaScript automatically calls the dogToString() function, which returns the string.
    : The toString() method takes no arguments and should return a string

 

: Since ES5, toString() called 
  - on null returns [object Null]
  - on undefined returns [object Undefined] 

toString.call(undefined);   // [object Undefined]
toString.call(null);        // [object Null]

 

@

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString


Object.prototype.valueOf()

1. method 

2. returns the primitive value of the specified object.
3. usually called automatically by JavaScript behind the scenes, and not explicitly in code.

    : JavaScript calls the valueOf method to convert an object to a primitive value.
    : rarely need to invoke the valueOf method yourself.
4. inherited by every object descended from Object.
5. can Overriding valueOf for custom objects
    : create a function to be called in place of the default valueOf method. Your function must take no arguments.
    
: check the details in MDN doc

[Syntax]

object.valueOf()

 

[Return Value]

The primitive value of the specified object.
If an object has no primitive value, valueOf returns the object itself.

 

@

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf

'#dev > 개념정리' 카테고리의 다른 글

JS: The Arguments Object  (0) 2020.05.14
JS: How to remove duplicates from an Array  (0) 2020.05.13
JS: call() and apply()  (0) 2020.05.13
for...in & for...of  (0) 2020.05.13
JS: Object.keys() & Object.values() & Object.entries()  (0) 2020.05.12
Comments