1 minute read

Wrapper object

The wrapper object wraps an original type like number, string, boolean, symbol. (cf. Javascript have 6-primitive types : string, number, boolean, null, undefined, symbol) It is created when a property is referenced, and disappears when the property reference is finished.

Below codes are simple example which help us to understand the properties of the wrapper object.

var a = 'This is a string'; 
a.len = 16; // new String(a).len = 16 --> not consisted
console.log(a.len); //undefined --> we cannot input something on 'a'
var a = 'This is a string';
var b = new String('This is a string');

console.log(typeof a);  // string
console.log(typeof b);  // real object

Object

It’s the highest class over all classes. Therefore, all classes inherit from object. And all classes can use the methods of the Object class. The object generates wrapper object.

  • When it operates, it gives empty object if the given value is null or undefined.
  • Otherwise, if the value is already an object, the value is returned as is.

Object generator has the size of ‘1’.

console.log(Object.length);  // 1

Object - example : Object.keys()

It serves to return the key value of the given object. It could be used as below code.

Object.keys(obj)

Below one is an simple example.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>TEST</title>
</head>
<body>
<script>

const data = {
    name1: 'Fault-a1' ,
    name2: 'Fault-a2',
    name3: 'Fault-b1',
    name4: 'Fault-b2',
    name5: 'Fault-b3',
    name6: 'Fault-c1',
    name7: 'Fault-c2',
    name8: 'Fault-c3'
};
console.log(Object.keys(data));

</script>
</body>
</html>

image

An array consisting of [] also returns the key value as shown below. (from 0 ~)

const arr = ['A', 'B', 'C', 'D'];
console.log(Object.keys(arr)); // console: ['0', '1', '2', '3']

Object - example 2 : Object.values()

It returns an array of values which is properties of the transfered parameter object.

const data = {
    name1: 'Fault-a1' ,
    name2: 'Fault-a2',
    name3: 'Fault-b1',
    name4: 'Fault-b2',
    name5: 'Fault-b3',
    name6: 'Fault-c1',
    name7: 'Fault-c2',
    name8: 'Fault-c3'
};
console.log(Object.values(data));  // 

image

Source

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object

https://includestdio.tistory.com/26