JavaScript "Not" everything is Object

Source: Internet
Author: User
Tags string methods

On the internet very much is saying "javascript everything is Object", then this "everything" is actually "everything", or an imaginary number? Also have read the article on the net, some three paper no donkey, is to copy the concept, some also just explained part, and did not give the exact conclusion. Then I want to combine my own understanding and give a definite conclusion.

  Of course, the conclusion, naturally, should be put at the end of the article. The little friends went on looking down.   Data type summary before entering, it is necessary to briefly describe the data type JS, of course, the small partners can also pass directly. There are two types of data for JS: Primitive Type and object type. The primitive types also include the following types: Numeric, String, Boolean, NULL, and undefined. Object types include special objects such as objects that users create themselves through various methods and arrays of functions.   Original value original value cannot be modified speaking of the original value, it is necessary to briefly talk about the original value of something.   first comes to the conclusion that:  primitive values are not modifiable, what is what, even forcing is forced to do not (this is stubborn). Numbers, Booleans, nulls, undefined obviously cannot be modified, 5 is 5, it cannot be 6, but for strings there may be some small partners wondering: How can a string not be modified, so many string methods are able to modify the string. For the sake of explanation, let's have a warm example first.  var string= ' The handsome has five girlfriend. '; String.touppercase (); we defined a "handsome man with five girlfriends" string (in fact, there are five girls, the diors has five girlfriend AH), and then called toUpperCase () method to capitalize the string. We output:  alert (' The original string is: ' + string + ' modified string is: ' + string.touppercase ()); OK, the output is this:  the original string is: the handsome Have five girlfriend. The modified string is: The handsome has vife girlfriend. See here, everything is naked.:  looks like the string method returns the modified string, actually returning a new string, which is two strings, Zaokangzhiqi is still on the couch (other ways small partners can also try).   So, when we talk about this, we can see that the original value is really not modifiable, that is, the original value is immutable.   Original value is the object? Next, let's cut to the chase: is the original value the object?   First, the object type is naturally the object, as for the reason, that is, "prove my mother is my mother." As to whether the original type is an object, we first describe the string type.   A fresh, bloody example first: var string= ' make life Get Better ';//Is this not MLGB!!!!!!!!! alert (string.length); This MLGB is a string, not supposed to have attributes like an object, but the truth? Let's take a look at this fresh, bloody output:  20 The result is that the original value string does use the length property of the string object, outputting the string string.  ok, and it is because of this that we have reason to think of string types as objects. Then why is this? This involves the concept of the wrapper object.   Wrapper object In fact, in the above example, when reading a string will create an object, but this object is only temporary, so we call it a temporary object, the academic name is called the wrapper object. Say it is temporary, because when we read its properties, JS will use the string string to create a string object through the new string (), the object will naturally have properties, but this object is only temporary, once the reference is over, the object is destroyed.   then cite a word from the JavaScript Authority Guide to add that:  actually (the wrapper object) does not necessarily create or destroy the temporary object in the implementation, but the whole process looks like this.   Similarly, numbers, Booleans can use their own constructors to create their own temporary objects when reading attributes, and reference their properties like objects (that is, an object), so strings, numbers, and Booleans can all be considered objects, note that this is considered an object, They are not real objects, strictly speaking, they are not objects, because the objects are mutable, they can be modified, and the original values are immutable and non-modifiable (see above), but their wrapper objects and objects are very similar, can read the properties of the wrapper object, so we just say, can be regarded as objects.   But notice a little bit of:  because it's just a temporary object, the Write property only exists on the temporary object, and then it is destroyed after the reference, and the properties of the write naturally vanish, with, Mao is the truth. The temporary object that is created can only read properties and cannot be written. Interested can assign a property and then read the attribute to see. In order to save you a few bytes of traffic, the example is not written here.  null&undefined, what about null and undefined? Let's keep touching.   First is null,null is a keyword, which means "null", we perform a typeof operation on NULL, the output is object, so we can think of NULL as a special object, this objectRepresents an "empty object" (suddenly thought of horse), even if it is an "empty object", it is also an object. This little companion can understand.   Then undefined,undefined is another that represents the "null" special value, which represents undefined, and when we declare a variable with no initialization, the output is undefined, and when we reference a non-existent property, the output is also undefined, Small partners can authenticate themselves, but note that it is not a keyword, it is a variable, and it is a global variable, and we can verify that:  alert (undefined in window) output is:  True turns out that undefined is indeed a global variable.   Similarly, we use typeof to look at the type of undefined, output is undefined. This strictly indicates that undefined is the only member of this type, incidentally, that the null typeof is an object, but that NULL is the only member of its own type. But undefined does not wrap objects, the typeof type is not object, and the object does not have a turns out relationship, so I do not think of it as objects.   A knot of course, the above is just a simple explanation, if the depth of the water to dig a lot (is not to say that the water is very deep suitable for some ...) , but it can be a simple conclusion: everything except Undefined,javascript is an object or can be seen as an object. Of course, this is only a opinion, and the small partners are welcome to share their understanding. I'll go, it's time for a nap. Sayonara

JavaScript is not "everything" object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.