Functions of JavaScript

Source: Internet
Author: User

JavaScript functions//code 1: ' Use strict ' function foo (x) {console.log (' x= ' + x);    for (var i = 0; i < arguments.length; i + +) {console.log (' arguments[' + i + ']= ' + arguments[i]); }};foo (1,2,3,4,5);//code 1 Explanation://1. Method can use more or less than the specified number of//2. '        Arguments ' keyword-like array, used to display input parameters//code 2: ' Use strict ' function func1 (A, B, c) {if (arguments.length = = = 2) {c = b;    b = null; } console.log (A + c);}; func1;//Code 2 Explanation://1. '    Arguments ' keyword is used to determine the number of parameters passed in, the above//code changes the parameter B to a variable parameter//code 3: ' Use strict ' function func1 (A, B, ... rest) {console.log (a);    Console.log (b); Console.log (rest);}; Func1 (1,2,3,4,5);//1//2//[3,4,5]func1 (1);//1//undefined//[]//Code 3 Explanation: The//1.ES6 standard introduces the ' rest ' keyword to get all remaining parameters//code 4: ' Use    Strict ' function sum (... rest) {var sum = 0;    for (var i = 0; i < rest.length; i + +) {sum + = Rest[i]; } return sum;}; var result = SUM (1,2,3,4,5), Console.log (Result),//15//Code 4 Explanation://1. The above method sum (... rest) function is to sum the input parameters//code 5: ' Use strict ' function fun1 () {var job = ' Cook ', desciption = ' Cook delicious food '; Console.log (Job + desciption);}; FUN1 ();//Code 5 Explanation://1. Because of the existence of variable promotion, the JS function is best to declare all variables//code 6:vara = 1,b = 2 in the starting position; [A, b] = [B, A];console.log (a); Console.log (b);//Application Example 1: Exchange variables A and B values var {hostname:domain, Pathname:path} = location;//    Application Example 2: Get the current page's domain name and path//code 6 Explanation://1. Application of Deconstruction Assignment//code 7:function Getage () {var year = new Date (). getFullYear (); return year-this.birthyear;} var li = {name: ' Lee ', birthyear:1991, Age:getage}var Ageli = Li.age (); Console.log (Ageli);//27ageli = Getage (); c Onsole.log (Ageli);//nanageli = Getage.apply (Li, []); Console.log (Ageli);//27//Code 7 Explanation://1. Use the Apply () method to change the point//code 8 for this:    ' Use strict ' var count = 0;var Oldparseint = Parseint;window.parseint = function () {count + = 1; Oldparseint.apply (null, arguments);} for (var i = 0; i < 5; i + +) {parseint (' 19 ');} Console.log (count);//code 8 Explanation://1. Replace the ' window ' object's function ' parseint ' to have a count function

  

Functions of JavaScript

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.