每天CookBook之JavaScript-041

  • 基于约定的私有变量
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>041</title>
</head>
<body>
    
</body>
<script type="text/javascript">
(function () {
    function Tune (song, aritst) {
        var title = song;
        this.concat = function(){
            return title + " " + aritst;
        }
    }

    var happySongs = [];
    happySongs[0] = new Tune("Putting on the Ritz", "Ella Fitzgerald");
    console.log(happySongs[0].title);
    console.log(happySongs[0].concat());

    happySongs[0].title = 'testing';
    console.log(happySongs[0].title);
})(); 

(function () {
    function Tune (song, aritst) {
        var _title = song;
        this.concat = function(){
            return _title + " " + aritst;
        }
    }

    var happySongs = [];
    happySongs[0] = new Tune("Putting on the Ritz", "Ella Fitzgerald");
    console.log(happySongs[0]._title);
    console.log(happySongs[0].concat());

    happySongs[0]._title = 'testing';
    console.log(happySongs[0]._title);
})(); 
</script>
</html>
posted @ 2016-07-18 23:02  4Thing  阅读(100)  评论(0编辑  收藏  举报