JavaScript Type & value & variable __javascript

Source: Internet
Author: User
Tags variable scope
positive 0 vs minus 0

Two values are almost identical except as divisor:

var zero = 0;       Positive 0
var negz =-0;      Minus 0
Zero = = Negz;      True
1/zero = = 1/negz;  False

"JavaScript Authority Guide" p.37

undefined VS null

Undefined is the default value before the variable is unassigned, meaning undefined. typeof undefined = undefined.
NULL is a null object reference that indicates that the object is empty. typeof Null = object.
For more information please refer to

string Length

String.Length () returns the number of 16-bit values, not the number of characters .

var p = "π";    16-bit inner code, 0x03c0
var e = "E";    17-bit internal code, 0x1d452
p.length;       1
e.length;       2

"JavaScript Authority Guide" p.38

Wrapping Objects

There are 2 types of data in JavaScript: The original type and the object type.
Number, String, Boolean, Null, undefined is the original type.

var s = "Hello world!";         S is the original type string
var word = s.substring (2, 5);   With the properties of string, Word is also an original type STIRNG (you can test with typeof)

Since the string is not an object, why are there attributes?
As long as the attribute of the string s is referenced, JavaScript is converted to an object by a new string (s) , and the new object is destroyed once the property reference is finished.

var s = "Test";     Create an original type string
S.len = 4;          A temporary object was created and a property was added to the temporary object, and the original value s did not change
var t = S.len;      T is undefined, because the original value does not change
var u = s.length;   U=4 because the temporary object string contains the Length property


var s = "Test";         typeof (s) = string
var n = 1;              typeof (N) = number
var b = true;           typeof (b) = Boolean
var S = new String (s);  typeof (S) = Object
var N = new number (n);  typeof (N) = Object
var B = Boolean (b);     typeof (B) = Object


Immutable Original value & mutable object reference

Original value:

var s = "Hello";    Defines an original type string
s.touppercase ();    Returns the temporary object "Hello" and does not change the original value S                   /S/value or Hello


Object reference:

var o = {x:1};    Defines a common object
o.x   = 2;          Modify the property value of an object
o.y   = 3;          Add a property

var a = [1,2,3];    Defines an array object
a[0]  = 0;          Modify the value of an array
a[3]  = 4;          Add an array element


var o = {x:1}, p = {x:1};      Defines 2 objects with the same attributes
o = = p;                            False, two separate objects are not equal to

var a = [];                         Defines an empty array object
var b = A;                          B with a reference to the same array
b[0]  = 1;                          Modify the value of the array
a[0];                               1
A = = = B;                            True, referencing the same object equally

"JavaScript Authority Guide" p.46

Explicit type conversions

If one operand of the "+" operator is a string, it converts another operand to a string .
The unary "+" operator (or the "-" operator) converts its operand to a number .
one dollar "!" Operator converts its operands to a boolean value and then takes the counter.

x + "";     Equivalent to STIRNG (x)
+x;         Equivalent to number (x), can also be written [x-0]
!! x;        Equivalent to Boolean (x), note double exclamation mark

"JavaScript Authority Guide" p.50

object converted to original value object into a string

The process of converting an object to a string is as follows: First call toString (), and if an original value is returned, the original value is converted to a string and returned. If the object does not have a ToString () method, or the method returns a value other than the original. valueof ()is invoked, and if the original value is returned, the original value is converted to a string and returned. If none of the above 2 methods can get an original value , a type error exception is thrown.

object into a number

The process of converting an object to a number is as follows: First call valueof (), and if you return an original value , convert the original value to a number and return. If the object does not have a valueof () method, or the method returns a value other than the original. The call toString ()is called, and if the original value is returned, the original value is converted to a number and returned. If none of the above 2 methods can get an original value , a type error exception is thrown.

object converted to Boolean

Object to a Boolean type: All objects are converted to "true".

For all non-date objects, the conversion of an object to its original value is essentially a conversion of an object to a number (first called valueof ()), and a Date object uses the pattern of a converted string.

var bool = new Boolean (false);
Alert (typeof (bool + 1));        Converts Boolean objects to numbers, number
alert (typeof (BOOL-1));        Number
alert (bool.tostring ());         "False"
alert ((1+bool). toString ());     1


var now = new Date ();
Alert (typeof (now + 1));         The date object is converted to a string, string
alert (typeof (Now-1));         Number
alert (now.tostring ());          "Sun May-2016 08:57:47 gmt+0800"
alert (now + 1). ToString ());    "Sun May 2016 08:57:47 gmt+08001"

"JavaScript Authority Guide" p.52

Variable Declaration Functional scopes (function scope)

In the C language, each piece of code within the curly braces has its own scope, which we call a block-level scope.
In JavaScript, a function scope is used, meaning that all variables declared within a function declaration are always visible in the body of the function.

function Test (o) {
    var i = 0;                      I have a definition in the entire function
    if (typeof o = = "Object") {
        var j = 0;                  J is defined for the entire function body
        (var k=0 k < k++) {//k is defined throughout the function
            Console.log (k);
        }

        Console.log (k);             Output
    }

    Console.log (j);                 J has been defined, but may not have been initialized
}

If a local variable has the same name as a global variable, the global variable is overwritten by a local variable.

var scope = "global";
function f () {
    console.log (scope);     Output "undefined" instead of "global" 
    var scope = "local";    Local variable initialization
    console.log (scope);     Output "local"
}

Because JavaScript does not have a block-level scope, you can place variable declarations at the top of the function so that the source code can clearly reflect the real variable scope.

Global Variables

When you declare a global variable, you are actually defining a property of the global object (this).
When a global variable is declared with Var, the variable is not configurable and is not deleted by the delete operator.
If it is not declared with VAR, the global variable is configurable.

var Truevar   = 1;  Declares a globally variable that cannot be deleted
Fakevar       = 2;  Declares a globally variable that can be deleted
this.fakevar2 = 3;  Ditto

Delete Truevar;     False, the variable has not been deleted delete
Fakevar;     True, the variable is deleted delete
fakevar2;    True, the variable is deleted


Scope chain (scope chain) is a list of function objects or lists that define variables in the scope of this code (closures). The order of objects in the list is: local object, Global object (Implementation of subsequent variable resolution). When JavaScript needs to find the variable x (this process is called "variable resolution" variable resolution), it starts looking from the first object in the chain, and if the object has a property named X, the value of the attribute is used directly. If it does not exist, the next object on the chain will continue to be found. When you define a function, it actually saves a scope chain. When this function is invoked , it creates a new object to store its local variables and adds the object to the saved scope chain, while creating a new, longer "chain" representing the scope of the function invocation .

"JavaScript Authority Guide" p.56

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.