zoukankan      html  css  js  c++  java
  • javascript 函数式编程

    简介

    一直不理解函数式编程,其实就像deeplearning 一样 知难行易。一般人使用的多的,不会太难;

    Example

        function SimpleWidget(spec) {
            var instance = {}; // <-- A
    
            var headline, description; // <-- B
    
            instance.render = function () {
                var div = d3.select('body').append("div");
    
                div.append("h3").text(headline); // <-- C
    
                div.attr("class", "box")
                   .attr("style", "color:" + spec.color) // <-- D
                   .append("p")
                       .text(description); // <-- E
    
                return instance; // <-- F
            };
    
            instance.headline = function (h) {
                if (!arguments.length) return headline; // <-- G
                headline = h;
                return instance; // <-- H
            };
    
            instance.description = function (d) {
                if (!arguments.length) return description;
                description = d;
                return instance;
            };
    
            return instance; // <-- I
        }// 逻辑上是  一个返回对象的函数 简称: 函数式编程 
    
        var widget = SimpleWidget({color: "#6495ed"})
                .headline("Simple Widget")// 返回对象之后然后对对象中的一些元素赋值
                .description("This is a simple widget demonstrating functional javascript.");
        widget.render();// 绘制
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    300. Longest Increasing Subsequence_算法有误
    LIS (DP)_代码
    pthread_detach pthread_create实例
    pthread_detach
    DP(动态规划)
    括号匹配(二)
    gdb调试遇到的问题
    matplotlib 显示中文
    一个奇怪的编码 big5-hkscs
    python 重载 __hash__ __eq__
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/12604152.html
Copyright © 2011-2022 走看看