美文网首页
读懂 Underscore.js

读懂 Underscore.js

作者: Zihowe | 来源:发表于2017-08-27 06:19 被阅读20次

    underscorejs 一共1500行的代码,很多functional programming的思想

    // underscore
    var arr6 = _.map(arr1, function(item) { return item * 3 });
    console.log(arr6);
    
    var arr7 = _.filter([2,3,4,5,6,7], function(item) { return item % 2 === 0; });
    console.log(arr7);
    
    var person = {
        firstname: 'Default',
        lastname: 'Default',
        getFullName: function() {
            return this.firstname + ' ' + this.lastname;  
        }
    }
    
    var john = {
        firstname: 'John',
        lastname: 'Doe'
    }
    
    // don't do this EVER! for demo purposes only!!!
    john.__proto__ = person;
    
    for (var prop in john) {
        if (john.hasOwnProperty(prop)) {
            console.log(prop + ': ' + john[prop]);
        }
    }
    
    var jane = {
        address: '111 Main St.',
        getFormalFullName: function() {
            return this.lastname + ', ' + this.firstname;   
        }
    }
    
    var jim = {
        getFirstName: function() {
            return firstname;   
        }
    }
    
    _.extend(john, jane, jim);
    
    console.log(john);
    

    Reference:
    http://underscorejs.org/docs/underscore.html
    https://www.udemy.com/understand-javascript/learn/v4/t/lecture/2562700?start=0

    相关文章

      网友评论

          本文标题:读懂 Underscore.js

          本文链接:https://www.haomeiwen.com/subject/yycbdxtx.html