JS/JavaScript

[JS] 09. Function

밍글링글링 2017. 8. 28.
728x90

09_Function.html

<script>
    //Function declaration.
    function foo(){ //옛날문법
        //Do something.
    }
    
    //Named function expression.
    var foo = function(){ //최신 문법
        //Do something.
    };
    
    //[Using Functions]
    //A simple function.
    var greet = function(person, greeting){
        var text = greeting+","+person;
        console.log(text);
    };
    greet("Rebecca", "Hello"); //"Hello, Rebecca"
    
    //a function that returns a value.
    var greet = function(person, greeting){
        var text = greeting +","person;
        return text;
    };
    console.log(greet("Rebecca", "Hello")); //"Hello, Rebecca"
    
    //A function that returns another function.
    var greet = function(person, greeting){
        var text = greeting + ","+person;
        return function(){
            console.log(text);
        };
    };
    var greeting = greet("Rebecca", "Hello");
    greeting(); //"Hello, Rebecca"
    
    //An immediately-invoked function expression.
    (function(){
        console.log("Hello World");
    })();
    
    //Passing an anonymous function as an argument.
    var myFn = function(fn){
        var result = fn();
        console.log(result());
    }
    //Logs "hello world"
    myFn(function(){
        return "hello world";
    })
            
    //Passing a named function as an argument
    var myFn = function(fn){
        var result = fn();
        console.log(result);
    };
    var myOtherFn = function(){
        return "hello world";
    };
    myFn(myOtherFn); //"hello world"
</script>
 

728x90

'JS > JavaScript' 카테고리의 다른 글

[JS] 11. This  (0) 2017.08.28
[JS] 10. TypeOf  (0) 2017.08.28
[JS] 08. Object  (0) 2017.08.28
[JS] 07. Array  (0) 2017.08.28
[JS] 06. Loop  (0) 2017.08.28

댓글