Read the summary of the knowledge points of JavaScript grammar essence

Source: Internet
Author: User
Tags joins shallow copy hasownproperty

Read the summary of the knowledge points of JavaScript grammar essence



Yesterday to soak up the majority of days of the book Hall, a breath of reading the "JavaScript Grammar essence" of the books, the overall book is still written well, no wonder so many recommendations. "The essence of JavaScript grammar" is mainly summed up and summed up the key knowledge of JavaScript, below I see I play after the more important knowledge points to share.

Key points of the JavaScript language essentials

One, more interesting recursive function

1. An interesting recursive, form data structure, 0,1,1,2,3,5,8,13 ... (Actually in my "javascript common knowledge point set" also wrote this recursion, in the bother to mention)

① Method One:

var Fibonacci = function (n) {
Return n<2? N:arguments.callee (n-1) + Arguments.callee (n-2);
Return n<2? N:fibonacci (n-1) +fibonacci (n-2);
}

② Closure Method implementation:


Copy Code
var Fibonacci = function () {
var memo = [0,1];
var fib = function () {
var result = Memo[n];
if (typeof result!== ' number ') {
result = FIB (n-1) + fib (n-2);
Memo[n] = result;
}
return result;
}
return fib;
}()

Copy Code

Second, regular part of the RegExp

(...) A capturing type grouping, or a word expression, captures the text that it matches and places it in the result array, with each capturing group assigned a number.
(?:) A non-capturing grouping that only makes simple matches and does not capture the matched text, performance is more efficient than capturing groups, and non-capturing groupings do not interfere with the numbering of captured groupings.
Regular escape, if the special character escapes, you can add a \ prefix to any special characters to make it literal, note \ prefix can not make letters or numbers literal. Common characters that need to be escaped-/[\] ^ {}, etc.

Iii. standard set of methods in JavaScript

①array

Array.concat (Item ...)
The Contcat method produces an array that contains a shallow copy of the array and appends one or more parameter item to the subsequent.

Array.join (separator)
The Join method constructs an array into a string. It first constructs each element in the array into a string, and then joins them together with a separator delimiter string.

Array.pop ()
The pop and push methods allow array arrays to work like stacks (stack). The Pop method removes the last element in the array and returns the element.

Array.push (Item ...)
The push method appends one or more parameter item to the end of an array, returning the new length value of the array.

Array.reverse ()
The Reseverse method reverses the order of the elements in the array and returns the array itself.

Array.shift ()
The shift method moves the first element in the array of arrays and returns the element. If the array is empty, it returns undefined. Shift is usually slower than pop.

Array.slice (Start,end)
The slice method makes a shallow copy of a paragraph in an array. Copy Array[start] First, copy to Array[end].

Array.Sort (COMPAREFN)
The sort method sorts the contents of an array. The sorting principle is the hash table, the sort method is not stable, and the stability of the order is that the equivalence in the sorted array is worth the relative position without changing, while the instability sort will change the equal worth relative position.

Array.splice (Start,deletecount,item,......)
The splice method removes one or more elements from the array and replaces them with a new item.

Array.unshift (Item ...)
The Unshift method, like the push method, is used to add elements to an array, but it is inserting the item into the beginning of the array instead of the layer.

  

②function

Function.apply (Thisarg, Argarray)
The Apply method invokes function, passing a binding to the object on this and an optional array as a parameter. The Apply method is used in the Apply invocation pattern.

Example Arg.concat (slice.apply (arguments,[0]));
Function.call (This)

③number

Number.toexponential (fractiondigits)
The Toexponential method converts this number to a string in exponential form. The optional parameter fractiondigits controls the number of digits after the decimal point. Its value must be in 0~20:

Document.writeln (Math.PI.toExponential (0)); 3e+0
Document.writeln (Math.PI.toExponential (2)); 3.14e+0
Document.writeln (Math.PI.toExponential (7)); 3.1415927e+0

Number.tofixed (fractiondigits)
The Tofixed method converts this number into a string in decimal form. Optional parameter FractionDigits controls the number of digits after the decimal point

Huaian whipped Egg net http://www.gamefrye.com/

Math.PI.toFixed (7)//3.1415927

Number.toprecision (Precision)
The Toprecision method converts this number into a string in decimal form. Optional parameter precision to control digital accuracy. Its value must be in the 0~21

Math.PI.toPrecision (7)//3.141593

Number.tostring (Radix)
The ToString method converts this number into a string. Optional parameter radix control cardinality. Its meaning must be 2~36.


Math.PI.toString (8)//3.110375724102643

④object

Object.hasownproperty (name)
If this object contains a property named name, then the hasOwnProperty method returns True. Properties with the same name in the prototype chain will not be checked. This method does not work when the name is "Hasownperperty" and returns false:

var a = {member:true};
var B = Object.create (a);
var t = a.hasownproperty (' member '); T is true
var u = b.hasownproperty (' member '); U is False
var v = b.member; True

  

⑤regexp

Regexp.exec (String)
The Exec method is the strongest and slowest way to use regular expressions. If it succeeds in matching regexp and string strings, it returns an array. The element labeled 0 in the array will contain a string that matches the regular expression RegExp. The element labeled 1 is the text captured by group 1, the element labeled 2 is the text captured by grouping 2, and so on. If the match fails, it returns NULL.

If RegExp comes with a G-id, things can get more complicated. The lookup does not start at the beginning of the string, but begins at the location of the Regexp.lastindex (the initial value is 0). If the match succeeds, then Regexp.lastindex will be set to the position of the first character after the match. An unsuccessful match resets the Regexp.lastindex to 0.

Regexp.test (String)
The test method is the simplest and quickest way to use regular expressions. If the regexp matches a string, it returns true; otherwise, it returns false.

⑥string

String.charat (POS)
The Charat method returns the character at the POS position in string. If the POS is less than 0 or greater than or equal to the length of the string string.length, it returns an empty string. JavaScript does not have a character type.

Example var name = ' Curly ';
var initial = Name.charat (0)//initial is ' C '

 

String.charcodeat (POS)

The Chartcodeat method is the same as Charat, except that it returns a character code point that is not a string, but an integer representation of the character at the POS position in string. If the POS is less than 0 or greater than or equal to the length of the string, Nan is returned.

Example var name = ' Curly ';
var initial = name.charcodeat (0); Initial is 67.

String.Concat (String ...)

The Concat method joins the other strings together to construct a new string. It is seldom used because it is more convenient to use the + operator.

var s= ' C '. Concat (' A ', ' t '); S is cat

  

String.IndexOf (searchstring,position)

The IndexOf method finds another string searchstring within a string. If it is found, returns the position of the 1th matching character, otherwise returns-1. optional parameter position can be set to start at a specified position in a string:

Example var text = ' Mississippi ', p = text.indexof (' ss ', 3);

String.LastIndexOf (search,position)

The LastIndexOf method is similar to indexof, except that it starts by looking at the end of the string instead of from the beginning.

String.localecompare (That)
The Localecompare method compares two strings.

Strng.match (RegExp)
The match method matches a string to a regular expression. It depends on the G-ID to determine how to match. If there is no G-ID, the result of calling String.match (RegExp) is the same as the result of calling Regexp.exec (string). However, if you have a G ID, it generates an array that contains all the matches except the capturing group.

String.Replace (Searchvalue,replacevalue)
The Replace method finds and replaces a string and returns a new string. The parameter searchvalue can be a string or a regular expression object.

String.search (RegExp)
The search method is similar to the IndexOf method except that it receives a regular expression object as a parameter instead of a string. If a match is found, it returns the first character position of the 1th match, or 1 if no match is found. This method ignores the G-ID and does not have a position parameter.

Example var text = ' And in it he says "any damn fool could ';
var pos = text.search (/["']/)

String.slice (Start,end)

The slice method copies a portion of a string to construct a new string. If the start parameter is a negative number, it is added to the sting.length. The end parameter is optional, and the default value is String.Length. If the end parameter is negative, it will be added to the string.length. The end parameter is equal to the position value of the last character you want to take plus 1. To get n characters starting at position p, use String.slice (p,p+n).

String.Split (Separator,limit)

The Split method splits the string into fragments to create an array of strings. The optional parameter limit limits the number of fragments to be split. The separator parameter can be a string or a regular expression.

Example var digits = ' 0123456789 ';
var a = Digits.split (', 5); A is [' 0, ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '];

String.substring (Start,end)
The use of substring is the same as the slice method, except that it cannot handle negative parameters. There is no reason to use the substring method. Please replace it with slice.


String.tolocalelowercase ()
The toLocaleLowerCase method returns a new string that uses localized rules to convert all the letters in this string to lowercase format.

String.tolocaleuppercase ()
The toLocaleUpperCase () method returns a new string that uses localization rules to convert all the letters in this string to uppercase format.

String.tolowercase ()
The toLowerCase method returns a new string in which all letters in the string are converted to lowercase.

String.touppercase ()
The toUpperCase method returns a new string in which all letters in the string are converted to uppercase format.

String.fromCharCode (char ...)
The String.fromCharCode function returns a string based on a serial number encoding

var a=string.fromcharcode (67,97,116); A is ' Cat '

Iv. Some problems of type detection and conversion

typeof
The typeof operator returns a string that identifies the type of operand.
typeof Null detects that it is not null, but ' object '
Typeof/a/usually detects the result as ' object ', but in the Safari 3.x version series, returns ' function '
A method of detecting an array by typeof, although unstable, is worth reference to learning:

if (my_value && typeof my_value = = = ' object ' && my_value.constructor = = = Array) {
My_value is an array
}

  

NaN
Nan is a special numerical value that represents not a number, but when we use typeof detection, we find a strange phenomenon:
typeof NaN = = = ' number '; Result is True
typeof cannot discern numbers and Nan, and Nan is not equal to its own
Nan = = = Nan//false
Nan!== nan//true
JavaScript provides a isNaN function that distinguishes between a number and Nan, and the isNaN () method detects a strongly typed conversion of an incoming parameter:
IsNaN (NaN)//true
IsNaN (0)//false
IsNaN (' oops ')//true strong type conversion
IsNaN (' 0 ')//false strong type conversion
Determine if the Isnumber function is a number:

var isnumber = function (value) {
return typeof value = = = ' Number ' && isfinite (value);
}

  

Pseudo-Array
JavaScript does not have a real array. The array of JavaScript is really very easy to use. The base of the array we usually use is an object generated.
detecting arrays

if (Object.prototype.toString.call (my_value) = = = ' [Object Array] ') {
My_value is really an array
}

  

False value
JavaScript has an odd set of false values
Value type
0 Number
NaN (non-numeric) number
' (empty string) string
False Boolean
Null Object
Undefined undefined
These values are all equivalent to false, but are not interchangeable.

Five, = = and! =, = = = and!==, same type = = no conversion (note)


Copy Code
' = = ' 0 '//false
0 = = "//true"
0 = = ' 0 '//true

False = = ' false '//false
false = = ' 0 '//true

false = = undefined//false
false = = NULL//false
Null = = undefined//true

' \t\r\n ' = = False//true

Copy Code

Rules for the operation of "= =":

1. If the two operands are of the same type, follow strictly equal comparison rules. If they are strictly equal, then the comparison results are equal. If they are not strictly equal, the comparison results are unequal.

2. If the two operand types are different, the "= =" equality operator may also consider them to be equal. Detection of equality will follow the following rules and type conversions:

-if one is null and the other is undefined, then they are equal.

-If one value is a number and the other is a string, the string is first converted to a number and then compared using the converted value.

-If one of the values is true, it is converted to 1 and then compared. If one of the values is false, it is converted to 0 and then compared.

-If one value is an object, the other value is a number or string. Convert the object to the original value, and then compare it. The object is converted to the original value by the ToString () method or the ValueOf () method. The built-in class at the core of the JavaScript language first attempts to use valueof () and then attempts to use ToString (), except for the date class, which uses only the ToString () conversion. Objects that are not in the core of the JavaScript language are converted to original values through their own implementation definitions.

-comparisons between the other different types are not equal.

The last question, is the JS sort () Sort principle problem, as if it is a hash table, but also see is bubble, I hope which big God help me answer, thank you!

Simple, pfan!. Out of the mix, everything is to be also.


Read the summary of the knowledge points of JavaScript grammar essence

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.