Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Why JavaScript basic types can invoke methods     - Linux common network tools: Scan routing of mtr (Linux)

- Linux system monitoring, top command of the diagnostic tool Detailed (Linux)

- DIY security of Linux platform (Linux)

- Oracle 11gr2 new APPEND_VALUES tips (Database)

- cursor_sharing induced error ORA-00600 (Database)

- The best tools and techniques to find data on a Unix system (Linux)

- ARM platform compiler installation Golang (Linux)

- Change all child files and subdirectories in the owner permissions Ubuntu (Linux)

- Linux network security strategy (Linux)

- CentOS 6 kernel upgrade to Kernel 3.x (Linux)

- Laravel 4.2 Laravel5 comprehensive upgrade Raiders (Server)

- jQuery update the content and method of use 3.0 (Programming)

- findmnt- Looking mounted file system (Linux)

- Build your own Git server under Linux (Server)

- MySQL 5.7.10 source code for the latest version of the installation process in detail (Database)

- blecat: Bluetooth Gadgets (Linux)

- JavaScript is implemented without new keywords constructor (Programming)

- Hadoop vs spark (Server)

- CentOS 6 compiling httpd-2.4.10 (Server)

- Configuring DNS process under CentOS 6.5 (Server)

 
         
  Why JavaScript basic types can invoke methods
     
  Add Date : 2018-11-21      
         
         
         
  We start with a pen questions:

var str = 'string';
str.pro = 'hello';
console.log (str.pro + 'world');
Output What? To understand the problem, we have to begin at the beginning.

Javascript data types divided into two categories, basic types (or a primitive type) and reference types. The value of the basic types of simple data segment is stored in the stack memory, a total of five, according to the values ​​of access, which are undefined null boolean number and string; and the value of a reference type is the object stored in the heap memory, accessed by reference , the main Object Array Function RegExp Date and the like.

// Basic types
var a = 10;
var b = true;
var c = 'string';

// Reference type
var d = {};
var f = [];
var e = new String ( 'abc');
Let us look back at this road pen questions, it is clear that the variable str is a basic type, str.pro str looks to add an attribute, and so on, we seem only when str is an object to see if such a usage, seems to have become accustomed to the object key add key-value pairs, but the basic type is also okay?

The first issue aside, we return to the question in the title:

var str = 'string';
console.log (str.length); // 6
str variable length and no property, not that good and only objects to use, or [] to access the property value of it? Here we introduce a concept called basic package types. In addition Object Array and other reference types, in fact, there are three special reference type String Number and Boolean, we facilitate basic types of operations corresponding thereto, and they are the basic type of packaging. str as a basic type is no length property, but its basic package type String have ah, in fact, in the implementation of console.log (str.length) code, what happened was this:

Create an instance of type String
Method call on an instance of the specified
Examples of this destruction
So to get the length of the code string variable str, the internal implementation probably look like this:

var str = 'string';
var len = str.length;
console.log (len); // 6

var str = 'string';
var _str = new String (str);
var len = _str.length;
_str = null;
console.log (len); // 6
Then we go back to the beginning of the article's example, it is not hard to understand. When performing str.pro = 'hello', in fact, create an instance of Internal basic type of packaging, and to pro property assignment of this example is hello, destroyed immediately after the instance is created, the next time trying to get the value of str.pro , the basic package will create an instance of a type, apparently without pro attribute of the newly created instance, is undefined, so the final output undefinedworld. The following code is the same reason:

var str = 1;
str.pro = 2;
console.log (str.pro + 10); // NaN
With this concept wrapper object, a numeric string operation much more convenient!

The last quote from "Javascript Revelation," in the words:

When used for literal strings, numbers and Boolean values, only in this case the value of the object will be deemed to create the actual complex objects. In other words, before attempting to use associated with the constructor method or retrieve properties (such as var len = 'abc'.length), I have been using the original data type. When this happens, Javascript creates a wrapper object literal values ​​behind the scenes, so that the value is treated as an object. After calling the method, Javascript that is discarded wrapper object, the value returned literal type. This is the strings, numbers, Boolean values ​​are considered primitive data type reasons.
     
         
         
         
  More:      
 
- Ubuntu 15.04 using the Eclipse 4.4, Java 8 and WTP (Linux)
- Sudo and Root Account in Ubuntu related (Linux)
- CentOS static IP network configuration (Linux)
- Standard and IO redirection (Linux)
- Linux security configuration (Linux)
- Struts2 form of non-use component tags (Programming)
- Linux 4.0+ kernel support for hardware switching module (HW Switch Offload) (Linux)
- Linux disk partition, format, mount the directory (Linux)
- Python2.7.7 source code analysis (Programming)
- RedHat Linux 5.5 installation process SVN Service Notes (Server)
- Linux environment variable configuration and save places (Linux)
- A brief introduction to some important Docker commands (Server)
- How to view the Linux QPS (Linux)
- To install Scribus 1.4.4 under ubuntu (Linux)
- Linux System Getting Started tutorial: Ubuntu desktop using the command line to change the system proxy settings (Linux)
- Oracle 12C truncate table cascade (Database)
- Boot-Repair Tool - repair of frequent start-up problems (Linux)
- Linux Demo dd IO test (Linux)
- Attic-- delete duplicate data backup program (Linux)
- Oracle online redefinition (Database)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.