[Effective JavaScript notes] 4th: Primitive types better than closed objects

Source: Internet
Author: User

JS has 5 primitive value types: Boolean, number, string, NULL, and undefined.

Check with typeof:

typeof true; "Boolean"

typeof 2; "Number"

typeof "S";//"string"

typeof null;//"Object": ECMAScript describes null as a unique type, but the return value is an object type, a bit confusing.

You can use Object.prototype.toString.call (null);//"[Object NULL]" to correctly determine the null type.

typeof undefined;//"undefined"

The standard library provides constructors to encapsulate Boolean values, numbers, and strings as objects.

var s1= "Hello";

is created as a constructor method.

var s2=new String ("Hello");

The behavior of a string object is similar to its encapsulated string value.

1, you can use the Add operator

s1+ "World";//"Hello World"

s2+ "World";//"Hello World"

2. A string that can forge its index

s1[4];//"O"

s2[4];//"O"

Unlike the original string, the string Object S2 is the true object.

typeof s1;//"string"

typeof s2;//"Object"

Each string object is a separate object that is always equal to itself. The result is the same for non-strict equality operators.

var s3=new String ("Hello");

S2==s3;//false

Other:

S1==s2;//true

Because S2 invokes the ToString method for implicit conversions, see [effective JavaScript note] 3rd: Beware of implicit casts "

The main reason for their existence is their practical approach. With the addition of implicit casts, JS makes it easy to use these practical methods, here is another implicit conversion: When you extract a property and make a method call to the original value, it behaves as if the value was encapsulated with the corresponding object type.

Example: The prototype object of string has a touppercase method that converts the string to uppercase. You can also call this method on the original string.

"Hello". toUpperCase ();//"Hello"

Note: This implicit conversion can set properties on the original value, but it has no effect on it

"Hello". someproperty=17;

"Hello". someproperty;//undefined

Cause: Because each implicit conversion generates a completely new wrapper object, updating the first object does not have a lasting effect.

It is meaningless to set properties on the original value, but it is worthwhile to be aware of this behavior. The following bugs can be prevented from producing.

Originally you want to set a property for an object, but do not notice that it is actually a primitive value, the program just ignore this setting, continue to execute. This error is difficult to debug.

Tips

1. As an equality comparison, the encapsulated object of the original type behaves differently from its original value. The original value must be equal to the original value, encapsulating the object not equal to the same value as the encapsulated object.   "MM" = = "MM"; New string ("MM")! = new String ("MM")

2. Getting and setting properties of the original type implicitly creates the encapsulated object. A new encapsulated object is created each time, so the value set is not preserved.

[Effective JavaScript notes] 4th: Primitive types better than closed objects

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.