The value assigned to the primitive will be lost in PhpStorm when setting the value of String.locale

I use the internationalization library l10n.js , and it seems you cannot pass the value to the library to tell which language you want to run translations in, because it is trying to get the value from the client browser; so I was told if you really want to set the locale, you will need to do something like:

String.locale = 'en-GB';

In my case, I do:

String.locale = $('html').attr('lang');

... at the top of the file.

This works, however, a warning appears in PhpStorm that:

The value assigned to this primitive will be lost.

I checked this question and understood why they received the warning, but I'm not sure about my instance - it basically just tells me that the current value String.localewill be lost?

+4
source share
3 answers

It looks like a false alarm of an output system like PhpStorm.

Stringis not a primitive JavaScript language. It is safe to assign a value to a property Stringhere because a function Stringis that object.

I believe the key idea Value assigned to this primitive will be lostcan be described using code:

> var primitive = "somestring";
> primitive.someprop = 3;
3
> primitive.someprop; 
undefined

JavaScript 5 : undefined, null, boolean, string number. , . JavaScript , . . , , .

+5

PHPStorm String. locale, ​​ String.prototype. .

var index = 1;
function test(value) {
	document.body.innerHTML += index++ + " " + value;
  document.body.innerHTML += "</br>";
}

test(String.locale); // 1 - check if locale exists

String.locale = "locale";
test(String.locale); // 2 - should return "locale"

String.prototype.locale = "other locale";
test(String.locale); // 3 - should strill return "locale"
test(String.prototype.locale); // 4 - should return "other locale"

test("".locale); // 5 - "other locale"
test(String("").locale); // 6 - "other locale"
test((new String("")).locale);  // 7 - "other locale"

String.locale = "locale";
test((new String("")).locale); // 8 - still "other locale", "locale" variable is not used when creating objects from "String" thus it lost in all instances of this constructor
Hide result

:

, PHPStorm :

  • Boolean
  • null ( Null, null)

IDE . .

, . , PHPStorm.

: primitive.locale = "en-GB" primitive.locale, undefined. , .

, , , PHPStorm Number, String, Boolean null.

:

var someBoolean = true;
someBoolean.locale = "en-GB";
document.body.innerHTML += someBoolean.locale;

document.body.innerHTML += "</br>";

var someString = "test";
someString.locale = "en-GB"
document.body.innerHTML += someString.locale;
Hide result
+1

, , :

const testObj = {};
testObj.name.first = 'jane';

, 'name' , .

0

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


All Articles