When it is advisable to declare String, Number and Boolean as objects in Javascript

When should Javascript data types be declared as objects? They slow down the execution speed and create unpleasant side effects, so why is this possible?

According to W3Schools: http://www.w3schools.com/js/js_numbers.asp enter image description here Also from http://www.w3schools.com/js/js_datatypes.asp enter image description here

+4
source share
2 answers

Numbers, strings, and Booleans can be either primitives or objects. For example, you can create a string that is primitive, and you can create another that is an object:

var name = 'John Doe';
var email = new String('john@example.com');

, ( email) . - , . , . JavaScript . :

var name = 'John Doe'; // This is a primitive.
var email = 'john@example.com'; // This is an other primitive.

:

var to = name + ' <' + email + '>';

, , email :

var index = email.indexOf('@');

, . , JavaScript , .

+4

String, Number Boolean : 1. : , . . , . https://zeekat.nl/articles/constructors-considered-mildly-confusing.html

  1. : , , . :

    var x = "Hello"; var y = new String ( "Hello" ); console.log(x === y)//false, x String n y

    var x = new String ( "Hello" ); var y = new String ( "Hello" ); console.log( == );//false,

+1

Source: https://habr.com/ru/post/1547501/


All Articles