Dot operator in javascript

2020-07-20 05:10发布

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.

标签: javascript
6条回答
欢心
2楼-- · 2020-07-20 05:12

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.

查看更多
▲ chillily
3楼-- · 2020-07-20 05:12

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楼-- · 2020-07-20 05:15

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:

查看更多
Lonely孤独者°
5楼-- · 2020-07-20 05:25

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.

查看更多
手持菜刀,她持情操
6楼-- · 2020-07-20 05:33

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
查看更多
可以哭但决不认输i
7楼-- · 2020-07-20 05:34

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.

查看更多
登录 后发表回答