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.