JavaScript - Why can't I add new attributes to a “string” object?

后端 未结 6 1380
误落风尘
误落风尘 2020-12-19 14:10

I\'ve experimented with JavaScript and noticed this strange thing:

var s = \"hello world!\";
s.x = 5;
console.log(s.x); //undefined

Every t

6条回答
  •  误落风尘
    2020-12-19 15:10

    Primitives MDC docs are immutable.

    primitive, primitive value
    A data that is not an object and does not have any methods.
    JavaScript has 5 primitive datatypes: string, number, boolean, null, undefined.
    With the exception of null and undefined, all primitives values have object equivalents which wrap around the primitive values, e.g. a String object wraps around a string primitive.
    All primitives are immutable.

提交回复
热议问题