3

I think that the dot operator can be apply to object only for access to his properties. I.e. for instance

var o={prop:'property'};
alert(o.prop);//property

When JS executing alert(o.prop); first object which contains by reference o will found further will be found property wich contains by reference o.prop.

Let's write

simple='hello';
alert(simple.length);//5

I dont understand this. I'm excepted that in console will kind of error, because simple is no object.

2
  • 2
    Sure it is, simple is a string object. Everything in JavaScript is an object Commented Dec 18, 2013 at 12:35
  • 2
    @Adam I think you are not correct, number, string, boolean undefined, null are not object. Can you delete your comment. Commented Jan 25, 2020 at 16:58

6 Answers 6

7

I'm excepted that in console will kind of error, because simple is no object.

Actually, it is a String object. JS implicitly converts the primitive string type to a String object when you apply string methods to it.

It's wrong to say that "everything is an object" in JavaScript because there are primitive types that are not objects, including a string primitive. In many cases the implicit conversion between primitives and objects can easily obscure the fact.

Sign up to request clarification or add additional context in comments.

Comments

5

Quote from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings.

1 Comment

In ECMA-262 reffered to [[Extensible]] internal property. Is it true that for all primitives converted to object JS automatically set [[Extensible]]=false;?
4

What's happening when you do something like simple.length is that, under-the-hood, the browser is temporarily converting simple into an object of type String and then looking up length on that. It's almost like calling new String(simple) except the browser doesn't actually need to create a new object, it just needs to behave as if it did. One thing to note is that this is all temporary:

simple.newProp = 123;   // will execute fine
simple.newProp;         // will return undefined

1 Comment

@SurajJain I can't remember exactly - it was a long time ago. Maybe it was 'Professional JavaScript for Web Developers' by Nicholas Zakas (his blog posts are great too). It's also probably in the language spec if you're feeling masochistic.
2

JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings.

So your simple var is a string instance, and it's inherits from String.prototype.

Ref:

Comments

1

String is an object inherent to JS. As such, it also has inherent properties, length being one of them. This property spits out the number of characters that are in a String object(as you've seen.)

If you wanted to you could override the inherent abilities of any inherent JS Object (String, Array, etc.) and create your own functionality, but that is generally a really bad idea.

Comments

1

In javascript, everything is an object.

  • {} === object
  • Array === object (an array, but still, an object, as it has properties which are normally not shown to you like length and indexOf(), etc)
  • string === object again! (that's where you get the string.length property from)

Checkout this list of objects JS has. Just because you can't see

{ }

does not mean that the variable is not an object. To get a list of properties of a data-type, you can write this in your console:

Object.getOwnPropertyNames(String)

And depending on what your browser supports, you'll get this:

[
 "prototype", "quote", "substring", "toLowerCase", "toUpperCase",
 "charAt", "charCodeAt", "contains", "indexOf", "lastIndexOf", 
 "startsWith", "endsWith", "trim", "trimLeft", "trimRight",
 "toLocaleLowerCase", "toLocaleUpperCase", "localeCompare",
 "match", "search", "replace", "split", "substr", "concat",
 "slice", "fromCharCode", "length", "name", "arguments", "caller"
]

4 Comments

Do you mean that when we write simple='hello' we have an object with [[Extensible]]=false; internal property
Your example disproves your point: try Object.getOwnPropertyNames( "hello" ).
@Juhana: the "hello" here is not a String object, it's a string primitive. It's the same for Numbers and Booleans. You'll have to try this: Object.getOwnPropertyNames(new String("hello")); which will return [length, 0, 1, 2, 3, 4]
Yeah, that's what I was saying. The OP does not have new String("hello"), they have just "hello". "In JavaScript, everything is an object" -- except things that are not objects, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.