JavaScript Advanced Programming Learning Notes Chapter fifth-reference types (function parts)

Source: Internet
Author: User
Tags rounds

Iv. type of function:

1. Method of function definition:

    • function declaration: Functions sum (NUM1, num2) {
      return NUM1 + num2;
      }
    • function expression: var sum = function (NUM1, num2) {
      return NUM1 + num2;
      };//notice there's a semicolon.
    • Constructor method: var sum = new Function ("Num1", "num2", "return NUM1 + num2");//

2. Duplicate declarations of functions are overwritten, and duplicate declarations of variables are ignored.

1 function addsomenumber (num) {2return num + +; 3 }4function  addsomenumber (num) {5return num + +; 6 }

The above code is equivalent to the following code, if the function signature as a function pointer, the equivalent of a function pointer from the first function to point to the second function, so the function of the duplicate declaration, the declaration will be declared after the overwrite.

1 var function (num) {2return num + +; 3 }; 4 function (num) {5return num + +; 6 };

3. The function declaration is not the same as the function expression, the parser first reads the function declaration and makes it available (accessible) before executing any code, and as for the function expression, it must wait until the parser executes to the line of code where it resides before it is actually interpreted.
4. Because the function name in ECMAScript itself is a variable, the function can also be used as a value. That is, not only can you pass a function to another function like passing parameters, but you can return a function as the result of another function.
5. Function Internal properties: Within the function, there are two special objects: arguments and this.

    • The arguments object also has a property called Callee, which is a pointer to the function that owns the arguments object.
    • The This object, which refers to the environment object to which the function is executed (not the Environment object at the time of the Declaration)
    • Properties of the function: Caller (function Call) This property holds a reference to the function that called the current function, and if the current function is called in the global scope, its value is null.

6. Properties and methods of the function:

  • Properties: Each function contains two properties: Length and prototype
    • The Length property represents the number of named arguments that the function expects to receive
    • Prototype property
  • Method: Each function contains two non-inherited methods: Apply () and call (). The purpose of both methods is to invoke the function in a specific scope, which is actually equivalent to setting the value of the This object in the function body.
    • Apply () function
      • Method receives two parameters: one is the scope in which the function is run, and the other is a parameter array. Where the second argument can be an instance of an Array, or it can be an arguments object.
    • Call () function
      • When using the call () method, the arguments passed to the function must be enumerated individually.
    • In fact, passing parameters is not the real place to apply () and call (); their real strength is the ability to expand the scope in which functions run. The biggest benefit of using call () (or apply ()) to extend the scope is that the object does not need to have any coupling with the method. For example:
    • 1Window.color = "Red";2 varo = {color: ' Blue ' };3 functionSaycolor () {4Alert This. color);5 }6Saycolor ();//Red7Saycolor.call ( This);//Red8Saycolor.call (window);//Red9Saycolor.call (o);//Blue

    • Bind () Function: This method creates an instance of a function whose this value is bound to the value passed to the bind () function. For example:

    • 1 window.color = "Red"; 2 var o = {color: "Blue" }; 3 function Saycolor () {4 alert (this. color); 5 }6var objectsaycolor = saycolor.bind (o); 7 // Blue

    • Each function inherits the toLocaleString () and toString () methods that always return the code of the function.

V. Basic PACKAGING Type: 3 Special Reference types: Boolean, number, and string

In fact, whenever a primitive type value is read, the background creates an object of the corresponding basic wrapper type, allowing us to invoke some methods to manipulate the data. The main difference between a reference type and a basic wrapper type is the lifetime of the object. An instance of a reference type created with the new operator is kept in memory until the execution flow leaves the current scope. Objects that are automatically created by the basic wrapper type exist only in one
The execution of the line code is instantaneous and is immediately destroyed. This means that we cannot add properties and methods to the base type values at run time.

1.Boolean Type:

Instances of the Boolean type override the ValueOf () method, return a base type value of TRUE or FALSE, override the ToString () method, and return the string "true" and "false".

There are two differences between a base type and a Boolean value of a reference type. First, the TypeOf operator returns "Boolean" to the base type, and "object" to the reference type. Second, because the Boolean object is an instance of the Boolean type, testing the Boolean object with the instanceof operator returns True, and the Boolean value of the test base type returns FALSE.


2.Number Type:

The number type also overrides the ValueOf (), tolocalestring (), and toString () methods. The overridden ValueOf () method returns the numeric value of the base type represented by the object, and the other two methods return a numeric value in the form of a string.

To format a numeric value as a string:

    • ToFixed (): A string representation of a numeric value that is returned by a specified number of decimal digits
    • Toexponential (): The method returns the string form of a numeric value expressed in exponential notation (also known as e notation).

3.String type Object:

Create wrapper type: Implemented with the String constructor, var stringobject = new String ("Hello World");

Each instance of the string type has a length property that indicates that the string contains more than one character. Even if the string contains double-byte characters (such as spaces), each character is still counted as one character.

Method:

  • The methods used to access specific characters in a string are: CharAt () and charCodeAt (). Both methods receive a parameter, which is based on the 0 character position. where the CharAt () method returns the character of the given position as a single character string, and charCodeAt () returns the character encoding for that position. For example:
  • 1 var stringvalue = "Hello World"; 2 // "E" 3 // Output "101"

  • Concat ()--stitching: used to stitch together one or more strings, returning a new string of stitching. does not affect the original string. For example:
    1 var stringvalue = "Hello"; 2 var result = Stringvalue.concat ("World"); 3 // "Hello World" 4 // "Hello"

  • Slice (), substr (), and substring ()-intercept: All three methods return a substring of the manipulated string, and also accept one or two parameters. The first parameter specifies the starting position of the substring, slice (), and
    The second parameter of substring () specifies the position after the last character of the substring. The second parameter of substr () specifies the number of characters returned. In cases where the arguments passed to these methods are negative, they behave differently. where the slice () method adds the passed-in negative value to the length of the string, and the substr () method adds the negative first argument to the length of the string, and the second argument with a negative is converted to 0. Finally, the substring () method converts all negative arguments to 0. does not affect the original string.
  • 1 varstringvalue = "Hello World";2Alert (Stringvalue.slice (3));//"Lo World"3Alert (stringvalue.substring (3));//"Lo World"4Alert (STRINGVALUE.SUBSTR (3));//"Lo World"5Alert (Stringvalue.slice (3, 7));//"Lo W"6Alert (stringvalue.substring (3,7));//"Lo W"7Alert (STRINGVALUE.SUBSTR (3, 7));//"Lo worl"8Alert (Stringvalue.slice (-3));//"Rld"9Alert (Stringvalue.substring (-3));//"Hello World"TenAlert (STRINGVALUE.SUBSTR (-3));//"Rld" OneAlert (Stringvalue.slice (3,-4));//"Lo W" AAlert (stringvalue.substring (3,-4));//"Hel" -Alert (STRINGVALUE.SUBSTR (3,-4));//"" (empty string)



  • IndexOf () and LastIndexOf (): Similar to array methods
  • Trim (): Creates a copy of a string, removes all whitespace from the predecessor and suffix, and returns the result. does not affect the original string.
  • There are 4 uppercase and lowercase conversions: tolowercase (), toLocaleLowerCase (), toUpperCase () and toLocaleUpperCase (), toLocaleLowerCase (), and The toLocaleUpperCase () method is for a specific region of implementation.
  • Match (): Calling this method on a string is essentially the same as calling RegExp's exec () method. The match () method accepts only one parameter, either a regular expression or a RegExp object.
  • Search (): accepts a parameter, either a regular expression or a RegExp object. Returns the index of the first occurrence in a string, or 1 if no match is found. Also, the search () method always looks backward from the beginning of the string.
  • Replace (): substitution accepts two parameters: the first argument can be a RegExp object or a string (the string will not be converted to a regular expression), the second parameter
    A number can be a string or a function. If the first argument is a string, only the first substring is replaced. The only way to replace all substrings is to provide a regular expression, and to specify the Global (g) flag. For example:

  • 1 var text = "cat, bat, Sat, fat"; 2 var result = Text.replace ("At", "ond"); 3 // "cond, Bat, Sat, fat" 4 result = Text.replace (/at/g, "ond"); 5 // "Cond, Bond, Sond, fond"

  • Split (): Splits a string into multiple substrings based on the specified delimiter and places the result in an array. The delimiter can be a string, or it can be a RegExp object (this party
    The method does not treat the string as a regular expression). You can accept the optional second parameter, which specifies the size of the array.

  • Localecompare ()
  • fromCharCode (): Receives one or more character encodings and then converts them to a string.

Vii. monomer built-in objects:

1.Global objects:

The encodeURI () and encodeURIComponent () methods can encode URIs (Uniform resourceidentifiers, Universal Resource Identifiers) for sending to the browser. A valid URI cannot contain certain characters, such as spaces. And these two URI encoding methods can encode the URI, they replace all invalid characters with special UTF-8 encoding, so that the browser can accept and understand.

where encodeURI () is used primarily for the entire URI (for example, Http://www.wrox.com/illegal value.htm), and encodeURIComponent () is used primarily for a segment of the URI (for example, the preceding URI Illegal value.htm) is encoded.


The main difference is that encodeURI () does not encode special characters that are themselves URIs, such as colons, forward slashes, question marks, and well sizes, while encodeuricomponent () encodes any non-standard characters it discovers.

Eval (): The eval () method is like a complete ECMAScript parser that accepts only one parameter, the ECMAScript (or JavaScript) string to execute. It is not recommended because the static scope is confusing.

Window object

Math object:

The min () and Max () methods are used to determine the minimum and maximum values in a set of values.

Math.ceil () performs an upward rounding, that is, it always rounds the value up to the nearest integer

Math.floor () Performs a downward rounding, that is, it always rounds the value down to the nearest integer

Math.Round () performs a standard rounding, that is, it always rounds a value to the nearest integer

The Math.random () method returns a random number greater than or equal to 0 less than 1

JavaScript Advanced Programming Learning Notes Chapter fifth-reference types (function parts)

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.