JavaScript

arguments, function

Naranjito 2021. 1. 11. 15:03

Arguments : An array-like object which accessible inside functions that contains the value of the arguments passed to that function

 

Function : A sort of object, and object has property. If property has function, we can call this property as method. 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <title>Document</title>
</head>
<body>
    <script>
        function sum(){ //function sum, () is parameter 
            //if there is no parameter(), or it is different from the number of parameters, it can be
            var i, _sum=0;
            for(i=0; i<arguments.length;i++){
                document.write(i+arguments[i]+'<br />');
                _sum+=arguments[i];
            }
            return _sum;
        }
        document.write(sum(1,2,3,4)); //arguments 1,2,3,4 
        //it is okay if there is different number between parameter and arguments

        function one(arg1){
            console.log(
                'one.length', one.length, //one is funtion, how many function of this name? 1
                'arguments', arguments.length //how many arguments it have? 2
            );
        }
        one('val1','val2');

        function func(){ //func is a function, it is an object therefore it can have method which is built-in methods(native code) by default Javascript provides

        }
        o1={val1:1, val2:2, val3:3}
        o2={v1:10, v2:20, v3:30, v4:40}
        function sum(){
            var _sum=0;
            for(name in this){//this means var this=o1;
                _sum+=this[name];
            }
            return _sum;
        }
        alert(sum.apply(o1))//the moment this is executed, sum method executing of object o1
        alert(sum.apply(o2))
    </script>
</body>
</html> 

'JavaScript' 카테고리의 다른 글

constructor, this  (0) 2021.01.12
object, encapsulation, information hiding, interface  (0) 2021.01.11
ajax, closure  (0) 2021.01.11
global variable, anonymous function  (0) 2021.01.07
global variable, local variable  (0) 2021.01.07