每天CookBook之JavaScript-042

  • 使用原型链创建对象
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>042</title>
</head>
<body>
    
</body>
<script type="text/javascript">
(function () {
    var str = 'one';
    String.prototype.exclaim = function(){
        if(this.length == 0) return this;
        return this + '!';
    }

    var str2 = 'two';

    console.log(str.exclaim());
    console.log(str2.exclaim());
})(); 

(function () {
    String.prototype.trim = function(){
        return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
    }
})(); 

(function () {
    function Tune (title, aritst) {
        this.concatTitleArtist = function(){
            return title + " " + aritst;
        }
    }s

    var happySongs = new Tune("Putting on the Ritz", "Ella Fitzgerald");
    Tune.prototype.addCategory = function(categoryName){
        this.category = categoryName;
    }

    happySongs.addCategory("Swing");
    var song = "Title and artist: " + happySongs.concatTitleArtist() + " Category: " + happySongs.category;
    console.log(song); 
})(); 
</script>
</html>
posted @ 2016-07-18 23:05  4Thing  阅读(91)  评论(0编辑  收藏  举报