|
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. |
|
|
|