Strings are not object then why do they have properties?

前端 未结 5 2014
孤城傲影
孤城傲影 2020-12-19 12:42

Code like this:

var str = \"Hello StackOverflow !\";
alert(typeof str);

gives me string as result. This means strings are not

相关标签:
5条回答
  • 2020-12-19 12:47

    Strings are actually a special kind of objects. You can't add properties to them, but you can modify the String.prototype, which is the prototype of any string variable, and add properties to it, like this:

    String.prototype.foo = 1;
    var a = 'hello';
    console.log(a.foo); // logs 1
    
    0 讨论(0)
  • 2020-12-19 12:51

    JavaScript is a prototype base language and you can define properties for your variables. Strings also have their predefined properties and methods to be used.

    more information

    0 讨论(0)
  • 2020-12-19 12:52

    Is string an object?

    This depends on how you defines object and what are you referring to when you say string. When you use the word string, you can be referring to just the primitive, or the wrapper object.

    What are primitives?

    In JavaScript there are 5 primitive types: undefined, null, boolean, string and number. Everything else is an object.

    Unlike objects, primitives don't really have properties. They exist as values. This explains why you cannot assign a property to a string:

    var archy = "hello";
    archy.say = "hello";
    console.log(archy.say); // undefined
    

    But sometimes manipulating a primitive would make one feel as if she/he is manipulating an object because primitives appear to have methods.

    var archy = "hello";
    console.log(archy.length); //5
    

    This is due to the fact that JavaScript creates a wrapper object when you attempt to access any property of a primitive.

    What are wrapper objects?

    Here is an extract from Javascript: The Definitive Guide

    The temporary objects created when you access a property of a string, number, or boolean are known as wrapper objects, and it may occasionally be necessary to distinguish a string value from a String object or a number or boolean value from a Number or Boolean object. Usually, however, wrapper objects can be considered an implementation detail and you don’t have to think about them. You just need to know that string, number, and boolean values differ from objects in that their properties are read-only and that you can’t define new properties on them.

    0 讨论(0)
  • 2020-12-19 12:57

    Strings are pure objects: http://www.ecma-international.org/ecma-262/5.1/#sec-15.5

    So the question is, what does the typeof operator. It simply acts according to its ECMA specification:

    http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.3

    0 讨论(0)
  • 2020-12-19 13:01

    A string like "Hello" is not an object in JavaScript, but when used in an expression like

    "Hello".indexOf(2)
    

    A new object derived from the constructor function String is produced wrapping the string "Hello". And indexOf is a property of String.prototype so things work as expected, even though there is a lot of magic going on.

    In the following case

    > var s = "xyz"; s.prop = 1; console.log(s.prop);
    undefined
    

    The reason you see undefined is that:

    1. The variable s is given a value which is a primitive string
    2. In s.prop = 1 and property named prop is assigned to a new, anonymous wrapper object.
    3. In the third statment above, another new object is created to wrap the primitive s. That is not the same wrapper object as in the second statement, and it does not have a prop property, so undefined is produced when asking for its value according to the basic JavaScript rules.
    0 讨论(0)
提交回复
热议问题