简体   繁体   中英

Some questions about JavaScript primitive data type

JavaScript has 7 primitive data types: string, number, boolean, null, undefined, symbol, bigint

I understand the three data types, string, number, boolean; I also understand bigint is introduced since number is not enough. One example why number is simple floating number:

var a = 1.000000000000001;
var b = 1.0000000000000001;
console.log(Number.isInteger(a)) //false
console.log(Number.isInteger(b))  // true

below are my questions:

  1. What is undefined? Is it the same as void in c++? but void is a keyword. why undefined is a primitive data type in Javascript?
  2. why null a data type? seems typeof null return is an object. what the difference between undefined and null? what the similar thing in c++?
  3. what is symbol? I read this link, What is the motivation for bringing Symbols to ES6? , can someone give me an example in which we really need a symbol?

Basically, in javascript we have two categories of types: Primitives and Objects(functions and array are also objects.). Primitives are copied by their values, objects are copied by their reference.

Undefined and void in c++ are not same. Javascript also has void operator, void operator in javascript and c++ is same, but comparing javascript properties to C++ is not something reasonable. C++ vs Java is more reasonable.

differences between null and undefined

1-

let name; 

this is undefined, we declared but did not return any value. A function returns undefined if a value is not returned We can also explicitly set this to undefined.

let name=null,

We use null in situations when we wanna clear the value of the variable.

2-we can use null in JSON but we cannot use undefined in JSON.

3-undefined is a type, while null is an empty object. Why null is an object, something needs to be solved by ECMA. It is a bug in javascript. Even the creator of javascript accepted that it was a mistake.

We use SYMBOLS to make properties or methods of an object private. So we hide the details and show only the essentials. It is called abstraction in javascript.

How to implement this:Let's create a simple class with "radius" property

class Circle {
    constructor(radius) {
        this.radius = radius; 
    }
    }

A symbol is essentially a unique identifier. Every time we call this function we get a unique identifier. It is not a constructor function,though.

Symbol()===Symbol() //will be false

Implementation:

const _radius=Symbol()
class Circle {
    constructor(radius) {
    this[_radius] = radius; //since property name starts with _, we use bracket notation
        }
         }

now test this. Create an instance of Circle:

const c=new Circle;
console.log(Object.getOwnPropertyNames(c))// you will see a number on the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM