每天CookBook之JavaScript-048

  • bind的使用
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>048</title>
</head>
<body>
    
</body>
<script type="text/javascript">
(function () {
    window.onload = function(){
        window.name = "window";

        var newObject = {
            name : "object",

            sayGreeting: function(){
                console.log("Now this is easy, " + this.name);
                nestedGreeting = function(greeting){
                    console.log(greeting + " " + this.name);
                }.bind(this);

                nestedGreeting("hello");
            }
        };
        newObject.sayGreeting("hello");
    }
})(); 
</script>
</html>
posted @ 2016-07-20 22:55  4Thing  阅读(125)  评论(0编辑  收藏  举报