Object Wrappers

It is possible to work with the value data types (string, number, boolean, symbol) as if they were objects.
Everytime you use a method or property on a value data type a special temporary object wrapper is created to perform the operation and is then automatically destroyed afterwards.
These object wrappers have the names String, Number, Boolean and Symbol.
It is possible to create these object wrappers directly using the new keyword but this is not recommended.

When a variable is declared with the keyword 'new', the variable is created as an object.

var a = new string() 
var a = new String() ??

var b = new number() 
var c = new boolean()

A data structure can be easily created.
You can use key-value pairs that are separated with a colon.

var myObject = { name:"Monday", value:42.5}; 

When you are reading from an object you need to use square brackets

var myValues = myObject["name"] + "day"; 

You can use square brackets to add or update properties

myObject["name"] = "Tuesday"; 
myObject["anothernewproperty"] = "some text";

delete keyword

The delete keyword can only be used to remove something from an object
You cannot delete variables or whole objects.
If you are not in strict mode trying to delete variables or objects does not generate an error, it just gets ignored.


Similar to array deconstructing except curly brackets are used instead of square brackets.

var { VariableName1, VariableName2 } = myObject 

instanceOf operator

Lets you check if an object has a specific object type.

var myString1 = new String("Mon"); 
myString1 instanceof String; // true

var myString2 "Tue";
myString2 instanceof String; // false

"wed" instanceof String // false
true instanceof Boolean // false

Evaluates to true if the object inherits from a class's prototype

class MyClass { } 
let x = new MyClass();
x instanceof MyClass // true

© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext