Basic data types and packaging types of JavaScript systems, javascript Data Types

Source: Internet
Author: User

Basic data types and packaging types of JavaScript systems, javascript Data Types

Preface

Javascript data types can be divided into two types: original type and reference type.

The original type is also known as the basic type or simple type. Because it occupies a fixed space and is a simple data segment, it is stored on the stack to facilitate variable query speed) ). The basic javascript data types include Undefined, Null, Boolean, Number, and String.

Because the value size of the reference type changes, it cannot be stored in the stack. Otherwise, the variable query speed is reduced. Therefore, it is stored in heap, the value stored in the variable is a pointer pointing to the memory of the storage object (access by address)

[Note] attributes and methods can be added to a value of the reference type, or its attributes and methods can be changed or deleted. However, attributes and methods cannot be added to basic types.

Undefined

The Undefined type has only one value, that is, undefined. When the declared variable is not initialized, the default value of this variable is undefined.

var test;//undefinedconsole.log(test == undefined);//truevar test = undefined;//undefined 

Only one operation can be performed on a variable that has not been declared. The typeof operator is used to detect its data type. However, errors may occur in strict mode.

Typeof (test); // undefined

Scenario

[1] variables with no value declared

[2] retrieving attributes that do not exist in an object

[3] execution results of functions without return values

[4] function parameters are not passed in

[5] void (expression)

Type conversion

  Boolean(undefined):  false  Number(undefined):  NaN  String(undefined):  'undefined'

Null

The Null type has only one value, which is null. Logically, the null value indicates a null Object Pointer. If the defined variable is used to save the object, it is best to initialize the variable to null. In fact, the undefined value is derived from the null value, so undefined = null

[Note] null is a null Object Pointer, and [] is an empty array, and {} is a null object. The three are different.

console.log(null == undefined);//true 

Scenario

When the object does not exist

Type conversion

Boolean (null): false
Number (null): 0
String (null): 'null'

[Note] Custom Attributes cannot be added because undefined and null are not constructors.

Packaging type

The packaging type is a special reference type. Every time you read a basic type value, the background will create a corresponding BASIC packaging type object, which may call some methods to operate on the data. The packaging types include Boolean, Number, and String.

Var s1 = 'some text'; var s2 = s1.substring (2); // in the preceding process, in fact, three steps are taken: var s1 = new String ('some text'); // (1) Create an instance of the String type var s2 = s1.substring (2 ); // (2) Call the specified method s1 = null on the instance; // (3) destroy the instance

[Note] the main difference between the reference type and the basic packaging type is the lifetime of the object. Instances of the reference type created using the new operator are stored in the memory until the execution stream leaves the current scope. The automatically created basic packaging type object only exists in the execution instant of a line of code and is immediately destroyed. This means that you cannot add attributes and methods for basic type values at runtime.

var s1 = 'some text';s1.color = 'red';alert(s1.color);//undefined 

Creation Method

There are two methods to explicitly create a package:

[1] Object method [not recommended]

var s = new Object('abc');var b = new Object(true);var n = new Object(123); 

[2] constructor mode [not recommended]

var s = new String('abc');var b = new Boolean(true);var n = new Number(123); 

[Note] using new to call constructors of the basic packaging type is different from calling transformation functions with the same name directly.

var value = '25';var number = Number(value);console.log(typeof number);//numbervar obj = new Number(value);console.log(typeof obj);//object 

Boolean

The Boolean type has only two values: true and false. The Boolean packaging type is the reference type corresponding to the Boolean value. Using a Boolean object in a Boolean expression may cause misunderstanding.

Scenario

[1] implicit type conversion caused by conditional statements

[2] literal or variable definition

Type conversion

Number (true): 1 | Number (false): 0
String (true): 'true' | String (false): 'false'

Boolean ()

Boolean (undefined): false
Boolean (null): false

Boolean (non-empty objects include empty arrays [] and empty objects {}): true

Boolean (not 0): true | Boolean (0 and NaN): false

Boolean (non-empty including space string): true | Boolean (''): false

[Note] true is not necessarily equal to 1, and false is not necessarily equal to 0.

Methods for inheriting packaging types

ValueOf (): returns the basic type value true or false.

ToString () and toLocaleString (): the return value is 'true' or 'false'

console.log(typeof true.valueOf(),true.valueOf());//boolean trueconsole.log(typeof false.valueOf(),false.valueOf());//boolean falseconsole.log(typeof true.toString(),true.toString());//String 'true'console.log(typeof false.toString(),false.toString());//String 'false'console.log(typeof true.toLocaleString(),true.toLocaleString());//String 'true'console.log(typeof false.toLocaleString(),false.toLocaleString());//String 'false' 

Number

Javascript has only one numeric type, which can represent both a 32-bit integer and a 64-bit floating point number.

For more information about the Number type, see

String

String type is the only primitive type in javascript that does not have a fixed size.

Next we will understand the packaging objects of basic JavaScript data types.

Symptom:Why can string operations use Object Notation?

For example:

var s    = "this is a String";      var len   = s.length; 

Resolution:

The three basic data types of JavaScript have corresponding object classes: Sring, Number, and Boolean;
JavaScript can flexibly convert one type of value to another type;
When we use a string in the object environment, that is, when we try to access the attributes or methods of this string;
JavaScript will internally create a String packaging object for this String value;
The String object will temporarily replace the original String value to complete our access;
This internally created String object exists instantly, so that we can access attributes and methods normally;
The String object will be discarded by the system after use;
The original value is not changed;

The preceding values also apply to numeric and boolean values;

Using the Object () function, any number, string, or Boolean value can be converted to its corresponding packaging Object;

For example:

var number_wrapper  = Object (3);
Articles you may be interested in:
  • Ext extension for basic types ext, extjs, format
  • Summary of "basic types" in JavaScript
  • Analysis of Class encapsulation Class libraries for javascript object-oriented Packaging
  • Basic Types and reference types of JavaScript
  • Basic javascript types
  • Values of the basic and reference types of JavaScript Data Types
  • Introduction to packaging types in Javascript
  • Introduction to basic javascript packaging types
  • Basic packaging types in javascript

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.