JavaScript you don't know Collation (v)-value vs. native function

Source: Internet
Author: User

First, the value

1) Number

JavaScript has only one numeric type: number, which includes "integers" and decimal numbers with decimals.

The syntax of the number var a = 5E10; 50000000000a.toexponential ();  "5e+10" var b = A * A;  2.5e+21 var c = 1/a; 2e-11var d = 0.42;var e =. 42; The number in front of 0 can omit var f = 42.; After the decimal point, the last 0 of the fractional part can also be omitted

Because numeric values can be encapsulated using the number object, numeric values can call methods in Number.prototype. For example, tofixed (..) method to specify the number of digits to display for the fractional part:

Invalid syntax: 42.toFixed (3);    syntaxerror//The following syntax is valid: a = ($). toFixed (3); "42.000" B = 0.42.toFixed (3); "0.420" C = 42..toFixed (3); "42.000" D = toFixed (3); "42.000"

2) Integer detection

Integer Detection if (! Number.isinteger) {  Number.isinteger = function (num) {    return typeof num = = "number" && num% 1 = = 0;  };} Safe integer Detection if (! Number.issafeinteger) {  Number.issafeinteger = function (num) {    return Number.isinteger (num) &&      Math.Abs (num) <= Number.max_safe_integer;}  ;}

3) null and undefined

Special numeric undefined and null. Their names are both type and value.

Null means null (empty value), has been assigned a value , but currently has no value. NULL is a special keyword, not an identifier , and we cannot use it as a variable and assign a value.

Undefined means no value (missing value), never assigned . Undefined is an identifier that can be used and assigned as a variable.

Use undefined as a variable and assign function foo () {"Using  strict";  var undefined = 2;  Console.log (undefined); 2}foo ();

4) Numbers that are not numbers

Nan means "not a number," which is interpreted as "invalid value", "Failed value", or "bad value", which may be more accurate.

Nan is a special value that is not equal to itself and is the only value that is non-reflexive (reflexive, that is, x = = = X is not true). and Nan! = Nan is true.

var a = 2/"foo"; nantypeof A = = = "Number"; Trueif (! Number.isnan) {  Number.isnan = function (n) {//non-numeric type values also return True return in IsNaN    (      typeof n = = = "Number") &&      Window.isnan (n)    );}  ;} Use Nan not equal to its own this feature if (! Number.isnan) {  Number.isnan = function (n) {    return n!== n;  };}

5) 0 Value

Addition and subtraction operations do not get negative 0 (negative zero). Negative 0 is typically displayed as "0" in the development debug console, but still appears as "0" in some older browsers.

0 value A = 0/-3; -0b = 0 *-3; -0c = + "-0"; -0d = Number ("0"); -0e = Json.parse ("-0"); -0//value comparison-0 = = 0; true-0 = = 0; True0 >-0; False//-0 's way of judging function Isnegzero (n) {  n = number (n);  return (n = = 0) && (1/n = = =-infinity);} Isnegzero (-0); Trueisnegzero (0/-3); Trueisnegzero (0); False

6) Special equation

The performance of Nan and-0 in equality comparisons is somewhat special.

Because Nan and itself are not equal, the Number.isnan (..) in ES6 must be used. And-0 equals 0 (and so for = = =), so we must use Isnegzero (..) Such a tool function.

Special Equation if (! object.is) {  object.is = function (v1, v2) {    //determine if -0    if (v1 = = 0 && v2 = = 0) {      return 1/v1 = = = 1/v2;    }    Determine if the Nan    if (v1!== v1) {      return v2!== v2;    }    Other case    return v1 = = = v2;}  ;}

7) Values and references

A JavaScript reference points to a value. If a value has 10 references, these references point to the same value, and they do not have a reference/point relationship to each other.

When you pass a value to a function, you actually pass a copy of the reference value, whether it is a base type or an object.

Basic type var a = 2;var B = A; B is a copy of the value of a b++;a; 2b; The 3//variable references the same value var c = [1, 2, 3];var d = C; D is a reference to [D.push] (4); c; [1,2,3,4]d; [1,2,3,4]//variables refer to different values var a = [1, 2, 3];var b = A;b = [4, 5, 6]; Assigns a value to B, references the new value, does not affect A's reference A; [1,2,3]b; [The 4,5,6]//function allows the parameter to re-reference the value function Foo2 (x) {  X.push (4);  X [1,2,3,4]    x = [4, 5, 6];//and then re-references the new value  X.push (7);  X [4,5,6,7]}var a = [1, 2, 3];//passes A to the function, actually assigns a copy of reference A to X, and a still points to [1,2,3]foo2 (a); A; is [1,2,3,4], not [4,5,6,7]

The above source code can be viewed at this time.

Second, the native function

Common native functions are: String (), number (), Boolean (), Array (), Object (), Function (), RegExp (), Date (), Error (), Symbol ().

1) Internal properties [[Class]]

This property cannot be accessed directly, generally through Object.prototype.toString (..) To view.

Internal properties [[Class]]object.prototype.tostring.call ([1, 2, 3]); "[Object Array]" Object.prototype.toString.call (/regex-literal/i); "[Object REGEXP]"//basic type Object.prototype.toString.call (NULL); "[Object Null]" Object.prototype.toString.call (undefined); "[Object Undefined]"

Although a native constructor such as null () and Undefined () does not exist, the internal [[Class]] property value is still "Null" and "Undefined".

Primitive type values are automatically wrapped by their own encapsulated objects, so their internal [[Class]] property values are "String", "number", and "Boolean", respectively.

Object.prototype.toString.call ("abc"); "[Object String]" Object.prototype.toString.call (42); "[Object number]" Object.prototype.toString.call (true); "[Object Boolean]"

2) Package Object Packaging

Because the base type value does not have the. length and. ToString () Properties and methods that need to be accessed by encapsulating the object, JavaScript automatically wraps a wrapper object for the base type value (box or wrap):

Package object var a = "abc"; a.length; 3a.touppercase (); "ABC"

If you want to encapsulate the base type value yourself, you can use the Object (..) function (without the new keyword)

Self-Encapsulation basic type var a = "abc"; var b = new String (a); var C = Object (a); typeof A; "string" typeof B; "Object" typeof C; "Object" b instanceof String; True C instanceof String; TrueObject.prototype.toString.call (b); "[Object String]" Object.prototype.toString.call (c); "[Object String]"

3) Unpacking

If you want to get the base type values in the encapsulated object, you can use the valueof () function:

Unpacking var a = new String ("abc"), var B = new number, var c = new Boolean (true); Console.log (a.valueof ()); "ABC" Console.log (B.valueof ()); 42console.log (C.valueof ()); True

Implicit unpacking occurs where the underlying type values in the encapsulated object need to be used.

JavaScript you don't know Collation (v)-value vs. native function

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.