JavaScript Basic Knowledge Collation

Source: Internet
Author: User
Tags array length type null

1. The main difference between null and undefined in JScript is that the operation of NULL is like the number 0, while the operation of undefined is like a special value Nan (not a number). The comparison of null values and undefined values is always equal.    null and undefined concatenation of string characters is equivalent to ' null ' and ' undefined ' string concatenation.    such as: null+2=2;        null*4=0;    Undefined+2=nan;    Null==undefined//returns TRUE.    null+ ' QQQQ ' =nullqqqq; undefined+ ' www ' =undefinedwww description: null only participates in the operation can be used as 0, but null==0, return false 2.JScript variable using VAR declaration, the operation of the automatic conversion type, the rule is as follows: Value + Word     string//To cast the value to a literal.     Boolean + string//To cast a Boolean value to a string.     Numeric + Boolean value//cast Boolean to numeric value. such as: 200+ ' hello ' = ' 200Hello ' true+ ' hello ' = ' Truehello ' true+5=6//true converted to 1 false*5=0//false converted to 0 3.Jscrip    T has three primary data types, two composite data types, and two special data types.        The primary (Basic) data type is: string//string numeric//number There is no difference between integers and floating-point values in JScript (all numeric values are represented as floating-point values within JScript). The Boolean//boolean compound (reference) data type is: The object array Special data type is: null//typeof null=object, not type NULL, this potential mixed     Xiao is for backwards compatibility (nul means "no value" or "no object") Undefined//typeof undefined=undefined, the object property does not exist, or the variable is declared but never assigned a value. Description: TypeOf return valueThere are six possible types: "Number," "String," "Boolean," "Object," "function," and "undefined." 4. Any expression with a value of 0, null, undefined (undefined), or an empty string ("or") is interpreted as false.    An expression of any other value is interpreted as true. For example: if (x=y+z)//assign Y+z to x first, and then determine if x is 0 5. Determine whether a variable is undefined: if (typeof (x) = = "undefined") the following two types are wrong: I F (x = = undefined) if (typeof (x) = = undefined) 6. Operator instanceof Operator: Returns a Boolean value that refers to the            Whether the object is an instance of a particular class.            Result = Object instanceof class in operator: The property exists in the test object.            Result = Property in Object ==,!=: Nan is not equal to any value including itself, that is, Nan!=nan, if it is determined to be Nan, use the Number.isnan () method. NULL is equal to null and undefined. Null==undefined Note: Basic string, numeric, and Boolean values are compared by value.                If their values are the same, the comparison results are equal. objects, including array, Function, String, number, Boolean, Error, date, and RegExp objects, are compared by reference.                Even if the two variables of these types have the same value, the result is true only if they are exactly the same object.                    such as: Var string1= "Hello";                    A two String object with the same value. Var StringObject1 = new String (string1);                    var StringObject2 = new String (string1);                    StringObject1 = = STRINGOBJECT2//returns FALSE.            stringobject1.valueof () = = STRINGOBJECT2//Returns True (ValueOf () returns the original value of the specified object) = = =,!== identity (strict equality) operator: the number and type are the same        7. The first type of statement control is the selection structure. Single choice structure (if), two-way selection structure (if/else), inline ternary operator?: Multi-channel selection structure (switch).        Using break to jump out of the second type of program control structure is a looping structure.     Tests the expression (while) at the beginning of the loop, tests the expression at the end of the loop (Do/while), operates on each property of the object (For/in), and loops (for) controlled by the counter.        such as: For...in loop (1) Traversal object properties: var myObject = new Object ();        Myobject.name = "James";        Myobject.age = "22";        Myobject.phone = "555 1234";//enumeration (looping) All properties of the object for (prop in myObject) {//Traverse MyObject All properties (Note that you must use prop traversal)           } (2) Traversing the key-value object: function Forindemo () {//create some variables.   var A, key, S = "";        Initializes the object. A = {"A": "Athens", "B": "BelGrade "," C ":" Cairo "}//Iteration properties.           For (key in a) {s + = A[key] + ";";       } return (s); Description: JS Other loops are: for, while, do. While, use break and continue to jump and continue to the next Loop 8.expando property: Object Extensible Property expando is not a keyword, but an abbreviation of expandable object, you can extend the object, JS all objects are expanded Show, there are two situations: first: If the name of the property conforms to the JS variable naming specification, you can use '. ': var myObj = new Object ();//Add two expando properties, ' name ' and ' age ' MyO        Bj.name = "Fred";    Myobj.age = 42; Second: If the property name is not a simple identifier, or if you do not know at the time of writing the script, you can use any expression in square brackets to index the property.        The names of all expando properties in Jscript are converted to string var MYOBJ = new Object () before being added to the object;        myobj["Not a valid identifier"] = "This is the property value";    MYOBJ[100] = "100"; Description: An array is also an object, except that it has a length property that other objects do not have.        Arrays can be implemented through the Expando property (although the array in JS does not support multidimensional), as follows: var myArray = new Array (3);        Myarray[0] = new Array (3);        MYARRAY[1] = new Array (3);        MYARRAY[2] = new Array (3); myarray[0][0]= ' myarray[0][1]= ' myarray[2][2]= ' 22 ' In addition, the array increases the expando attribute, lThe Ength property does not change, such as: var MyArray = new Array (3);        Myarray[0] = "Hello";        MYARRAY[1] = 42;        MYARRAY[2] = new Date (2000, 1, 1);        Myarray.length=3//Add some expando properties Myarray.expando = "jscript!";        myarray["another Expando"] = "Windows";    Myarray.length still returns a reference to an object prototype for the 3 9.prototype property, which can add functionality to an object prototype, and after the addition succeeds, all instances of the object have that property, including those that have already been generated.            such as://person contains two parameters of the constructor function person (name, sex) {this.name = name;        this.name = sex; } var nick=new person (' Nick ', ' Boy ') var iwen=new person (' Iwen ', ' gril ') iwen.hairstyle= ' long hair ';//Only Iwen    There are hairstyle properties person.prototype.height= ' ";//nick and Iwen instance objects increase the Height property Description: The prototype property can be used for the implementation of JS inheritance. 10. Internal object Jscript provides 11 internal (or "built-in") objects.    They are array, Boolean, Date, Function, Global, Math, Number, object, REGEXP, Error, String object.        The Array object var theMonths = new Array (12);        Themonths[0] = "Jan"; Themonths[1] = "Feb";        THEMONTHS[2] = "Mar";        THEMONTHS[3] = "APR";        THEMONTHS[4] = "may";        THEMONTHS[5] = "June";        THEMONTHS[6] = "Jul";        THEMONTHS[7] = "the";        THEMONTHS[8] = "SEP";        THEMONTHS[9] = "OCT";        THEMONTHS[10] = "Nov";    THEMONTHS[11] = "DEC";    Equivalent to var theMonths = new Array ("Jan", "Feb", "Mar", "APR", "may", "June", "Jul", "", "Sep", "Oct", "Nov", "Dec");       Note: If themonths.length=2, then the last 10 of the themonths array will be automatically deleted, leaving only the first 2 values. 11. Variable range The name of a local variable can be the same as a global variable, but this is a completely different and independent two variable. Therefore, changing the value of one variable does not affect the value of another variable.       Within a function that declares a local variable, only that local variable is meaningful.            such as: Var alec= "global variable";                function Changealec () {var alec= "local variable";                alec+= ", haha";            Alert ("Inside function:" +alec)//local variable, haha} changealec ();            alec+= ", Roar";    Alert ("Out of Function:" +alec);//global variable, Roar 12. Comparisons by value and by reference (1) Numbers and Boolean values (True and false) are copied, passed, and compared by value.    (2) objects, arrays, and functions are copied, passed, and compared by reference. (3) strings are copied and passed by reference, but are compared by value.    Note that if there are two string objects (created with new string ("something"), they are compared by reference, but if one or both are string values, they are compared by value.        Note: (1) to check whether the two arrays contain the same elements, you cannot directly compare the array objects, you need to compare the results of the toString () method. (2) Given the method of constructing the ASCII and ANSI character sets, uppercase letters in sequence order are preceded by lowercase letters (that is, lowercase > uppercase letters). such as "a" > "a", "C" > "a".    If you want to perform a case-insensitive match, you can call toUpperCase () or toLowerCase () on two strings.            such as: var arr1 = new Array (' A ', ' B ', ' C ');            var arr2 = new Array (' A ', ' B ', ' C ');                if (arr1 = = arr2) {alert (' array same ');//Does not execute} if (arr1.tostring () = = Arr2.tostring ()) {            Alert (' array toString () same ')//execute (arr1.tostring ())} var str1 = "AAA";            var str2 = "AAA";            if (str1 = = str2) {alert (' character same ')//execute} var strObj1 =new String ("AAA");            var strObj2 = new String ("AAA"); if (strObj1 = = strObj2) {alert (' character object same ')//does not execute}13. Passing parameters to a function to pass a parameter by value to a function is to make an independent copy of the parameter, that is, one that exists only in the TheA copy within a function. When passing objects and arrays by reference, the new value is not reflected outside the function if the original value is overwritten with the new value directly in the function.    It can only be seen outside the function if the object's properties or the elements of the array change.        such as://This code fragment destroys (overrides) its parameters, so the calling code does not reflect the change.             function clobber (param) {param = new Object ();//break parameter; not seen in the calling code.        Param.message = "This won't work";        }//This section of code changes the properties of the parameter and can see the property change in the calling code.        function Update (param) {param.message = "I was changed";//Change the properties of the object; You can see the change from the calling code.        }//Creates an object and assigns it to a property.        var obj = new Object ();                Obj.message = "This is the original"; Clobber (obj);//Call clobber, and output obj.message.        Note that it has not changed. Window.alert (Obj.message);                Still show "This is the original". Update (obj);//call Update and output obj.message.        Note that it has been changed. Window.alert (Obj.message); Show "I was changed". 14. Display information in the browser the Write () and Writeln () methods of the document are methods for adding content to the body, and the location to add is the location of the current execution document.write (if the 

  

JavaScript Basic Knowledge Collation

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.