JavaScript, Basic Package Type

In order to better basic value of the operation type, JavaScript offers three special reference types: Boolean, Number, and String. These reference types and traditions like objects have their own properties and methods, but also have their own special behavior.

A basic type of packaging Profile

  We know that the value of the basic types of properties and methods are not and can not be changed. However, the above three special reference type can impart a method of operation of the basic types of values:

1 var str1 = "hello world"2 var str2 = str1.sunstring(5);

  We know from the above code, str1 is a basic type of string, but we use it to call a method, and the return value is assigned to the str2. Fair to say primitive values ​​is no way, then this operation is how to achieve it?

  In fact, when reading a JavaScript primitive type value, the background will be as follows:

    1, create a basic package types (here String type) of an instance.

    2, call the relevant method on the instance.

    3, immediately destroy the instance.

  So in fact str1 when calling the method it can be understood as the implementation of the following procedure:

1 var s1 = new String(str1);
2 var str2 = s1.substring(5);
3 s1 = null;

  Note: The above operations are done in the background, we are not aware of it.

  The main difference between types of common references and basic type of packaging is that the life cycle of an object, use the new operator to create objects in the program execution flow before leaving the current scope has been present in memory, while the Basic package type created in the background there is only to perform an instant line of code, and then immediately destroyed. This means that we can add properties and methods for the value of the basic types, but in the end they will not be retained.

1 var str = "hello";
2 str.name = "someText";
3 str.say = function(){
4    //some code;
5 };
6 console.log(str.name);//undefined
7 str.say();//TypeError: str.say is not a function

  Look at the following code:

1 var str = new String("hello");
2 typeof str;//"object"
3 console.log(str);//{0:"h",1:"e",2:"l",3:"l",4:"o",length:5}

  It can be seen, although the type of object to create a basic package that can be displayed, but it's a complete departure from the original intention we use the string type for storing text information, so as not particularly necessary, we recommend not explicitly create and use it.

Two Boolean type

         Boolean type is a Boolean value corresponds to a reference type. To create a Boolean objects may look like this: 

1 var BooleanObj = new Boolean(false);

         But there is a problem, consider the following codes:

1 var booleanObj = new Boolean(false);
2 var result = booleanObj && true3 console.log(result);//true

         Although the value booleanObj is false, but it is itself an object, we know, will trigger an implicit type conversion when performing logical operations, all objects are converted to true, so the final result is true. But this is not the result we want, so it is best not to use Boolean objects at work.

Number three types

         Number is a number corresponding to the type of reference type. Can explicitly create a digital object, and as a Boolean, if it is created with the new new digital values, using the detected return typeof "object".

    Further Number object provides two practical ways:

    1,  toFixed()

    It takes a number as a parameter indicating the need to retain several decimal places, returns a string of numeric type.   

1 var num1 = 10.005;
2 console.log(num1.toFixed(2));//"10.01"

    2,  toExponential()

    Takes a number as a parameter, the fractional bits need to be retained, returns a digital representation of the exponentially, is a string type.

1 var num = 10;
2 console.log(num.toExponential(1));//”1.0e+1”

Four of type String

         String type is a string type of packaging. It can also explicitly is to create a string object with the new.

         Each instance of type String have a length property, indicating that the string contains the number of characters.   

1 var str = new String(“hello”);
2 console.log(str.length);//5

         String type provides the following common methods:

  1, charAt () and charCodeAt () - Get a single character position on the  

. 1  var str = "Hello World" ;
 2 the console.log (str.charAt (0)); // "H", the first character of str 
. 3 the console.log (str.charAt (str.length -. 1)) ; // "D", the last character of str 
. 4 the console.log (str.charCodeAt (0)); // 104, a first character encoding str 
. 5 the console.log (str.charCodeAt (str.length -. 1 )); // 100, the last character encoding of str

  2, concat () - splicing string

1 var str1 = "hello";
2 var str2 = "world";
3 console.log(str1.concat(str2));//"helloworld"
4 console.log(str2.concat(str1));//"worldhello"

  Part of a replication string - 3, substr (), substring () and slice ()

1 var str = "hello world";
2 console.log(str.substring(0,5));//"hello"
3 console.log(str.substring(0));//"hello world"
4 console.log(str.slice(6));//"world"
5 console.log(str.slice(4,7));//"o w"
6 console.log(str.substr(0,4));//"hello"
7 console.log(str.substr(0));//"hello world"

  As can be seen from the above example, the substring () and slice (), as can accept two parameters, the first parameter is the start position, the second parameter is the end position of the string in the eventual return of these two parameters intervals (does not include end position). substr () and they have a little different, the number of the second parameter is the need to intercept the string, rather than an end position of the index. They also have one thing in common: If you do not provide a second argument, then they are copied by default to the end.

  4, indexOf () and lastIndexOf () - Find the first occurrence of substrings in the string position

1 var str = "hello world";
2 console.log(str.indexOf("l"));//2
3 console.log(str.lastIndexOf("l"));//9

  indexOf () is a front to back look, lastIndexOf is a look from the back.

  5, toLowerCase () and toUpperCase () - case conversion

1 var str = "hello WORLD";
2 console.log(str.toLowerCase());//"hello world"
3 console.log(str.toUpperCase());//"HELLO WORLD"

  6, replace () - replacing parts of character

1 var str = "hello world,hello world,hello world";
2 console.log(str.replace("world","bokeyuan"));//"hello bokeyuan,hello world,hello world"
3 console.log(str.replace(/world/g,"bokeyuan"));//"hello bokeyuan,hello bokeyuan,hello bokeyuan"

  replace () accepts two parameters: a first parameter may be a string or a regular expression, a substring being sought. The second parameter may be a string or function (return value) indicating the replacement text. Function takes three parameters, the first one is the string to be replaced, and the second seats are present in the string, the third is itself the search string (str).

  7, split () - Split string

1 var str = "hello world";
2 console.log(str.split("l"));//["he", "", "o wor", "d"]

  Accepts a parameter, the parameter string appearing as a demarcation point division string, returns an array.

  8, fromCharCode () - converts the character into character encoding

1 var str = String.fromCharCode(65,66,67);
2 console.log(str);//"ABC"

  fromCharCode () is a static method of String, so it's a bit calls and other methods are not the same. It accepts one or more digits, character encoding them as, and converted into the corresponding characters.

Guess you like

Origin www.cnblogs.com/ruhaoren/p/11352134.html