The third chapter web front-end development engineer --JavaScript Advanced Programming 3-8 javascript function return values

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/wgf5845201314/article/details/90756929

                                           JavaScript function return value

 

This lesson is to talk:

  1. JavaScript function return value

2. JavaScript arguments objects

Speaker teachers: Head teacher

A function returns a value

With ginseng and takes no parameters and return values ​​are not defined, but called directly after execution. In fact, any function can be achieved with the return value back to the value returned by the return statement.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>js 函数返回值</title>
</head>
<body>
    <script>
        window.onload = function(){
            function fn1(a){
            //return 123+a;
            return function(b){
                alert(a+b); //这里是注释    
            }
            }
            //alert(fn1(456));   //这样调用不了   弹出整个函数体部分
        // fn1(2)(20);    //正确调用方式  结果22

            function $(id){
                return document.getElementById(id);
            };
            $('ad').onclick = function(){
                alert('节点的封装');
            }
       }
                 
    </script>
    <input type="button" id="ad" value="按钮">

</body>
</html>

Two .JavaScript arguments objects

        Do not mind how many function parameters passed in, it will not because the parameters are not uniform and error. In fact, the function body pass parameters may be received by the incoming arguments object.

function box(){
            return arguments[0] + '|' +arguments[1];
        }
        alert(box(1,2,3));   //输出结果   1|2  

arguments length attribute of the object can be obtained number of parameters.

function box() {
	return arguments.length;					//得到6
}
alert(box(1,2,3,4,5,6));

We can use the length property to determine how many intelligent argument, then the parameters of reasonable application. For example, to implement an adder, passed in all of the figures, the number of digits and uncertain.

Guess you like

Origin blog.csdn.net/wgf5845201314/article/details/90756929