Node.JS中类的导出语句

时间:2015-11-19 02:23:26

标签: javascript node.js

节点当前在严格模式下启用类构造。

如果我有以下课程:

$attendance = "2015-11-18 08:48:23";
$check_in = "08:00:00";
if ( strlen($check_in) <= 8 ) { // Does not include date. Prepend it automatically:
    $parts = explode(" ",$attendance);
    $attendance_date = $parts[0];
    $check_in = "{$attendance_date} {$check_in}";
}
$late = round((strtotime($check_in) - strtotime($attendance))/60,0);

可导出到另一个模块的相应导出语句是什么。另一个文件的import语句怎么样?

2 个答案:

答案 0 :(得分:2)

使用commonJS requiremodule.exports与目前在节点中“导入”或“导出”其他任何内容的方式相同:

Foo.js

class Foo {}
module.exports = Foo
// or if you want to edit additional objects:
// module.exports.Foo = Foo
// module.exports.theNumberThree = 3

Bar.js

var Foo = require("./Foo")
var foo = new Foo()

答案 1 :(得分:1)

这确实是Node支持ES6模块的问题。虽然它目前允许类,但ES6的导入/导出功能尚未实现,并且更多地依赖于CommonJS要求。

要导出,请使用以下内容:

int process_count = atoi(argv[2]);

if ( process_count < 1 )
    exit(EXIT_FAILURE);    // or similar

int p_id[process_count], io_burst[process_count], priority[process_count], cpu_burst[process_count];

导入:

<!DOCTYPE html>
            <html>
            <head>
                <meta charset="utf-8">
                <title>NVD3 Example</title>
                <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.css">
                <style>
                    .tick line {
                        display: none;
                    }

                    svg {
                        border-style: groove;
                        border-color: #999999;

                    }


                </style>
            </head>
            <body>
            <div ng-app="myApp" ng-controller="myCtrl">
                <select ng-model="graphType">
                    <option value="Monthly" label="Monthly" selected="selected">Monthly</option>
                    <option value="Customer">Customer</option>
                    <option value="Vendor">Vendor</option>
                    <option value="IndustrySegment">Industry Segment</option>
                    <option value="SalesEngineer">Sales Engineer</option>
                    <option value="CustomerType">Customer Type</option>
                </select>
                <button type="button" ng-click="changeData(graphType)">Change Graph Data</button>
                <button type="button" ng-click="refresh">Refresh</button>
                <section>
                    <nvd3 options="options" data="data"></nvd3>
                </section>
            </div>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
            <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
            <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.js"></script>
            <script type="text/javascript" src="./angular-nvd3.min.js"></script>
            <script type="text/javascript">

                var app = angular.module('myApp', ['nvd3']);
                app.controller('myCtrl', function ($scope) {

                    $scope.customerData = [
                        {
                            "key": "No. of Orders",
                            "color": "#d62728",
                            "values": [
                                {
                                    "label": "IC Pro",
                                    "value": -22
                                },
                                {
                                    "label": "Ilogicon",
                                    "value": -54
                                },
                                {
                                    "label": "Web India",
                                    "value": -5
                                }
                            ]
                        },
                        {
                            "key": "Revenue Generated",
                            "color": "#1f77b4",
                            "values": [
                                {
                                    "label": "IC Pro",
                                    "value": 47

                                },
                                {
                                    "label": "Ilogicon",
                                    "value": 53

                                },
                                {
                                    "label": "Web India",
                                    "value": 16

                                }
                            ]
                        }

                    ];
                    $scope.monthlyData = [
                        {
                            "key": "No. of Orders",
                            "color": "#d62728",
                            "values": [
                                {
                                    "label": "January",
                                    "value": -25
                                },
                                {
                                    "label": "February",
                                    "value": -43
                                },
                                {
                                    "label": "March",
                                    "value": -62
                                }
                            ]
                        },
                        {
                            "key": "Revenue Generated",
                            "color": "#1f77b4",
                            "values": [
                                {
                                    "label": "January",
                                    "value": 471

                                },
                                {
                                    "label": "February",
                                    "value": 987

                                },
                                {
                                    "label": "March",
                                    "value": 916

                                }
                            ]
                        }
                    ];
                    $scope.vendorData = [
                        {
                            "key": "No. of Orders",
                            "color": "#d62728",
                            "values": [
                                {
                                    "label": "Rockwell",
                                    "value": -22
                                },
                                {
                                    "label": "A",
                                    "value": -54
                                },
                                {
                                    "label": "B",
                                    "value": -5
                                }
                            ]
                        },
                        {
                            "key": "Revenue Generated",
                            "color": "#1f77b4",
                            "values": [
                                {
                                    "label": "Rockwell",
                                    "value": 47

                                },
                                {
                                    "label": "A",
                                    "value": 53

                                },
                                {
                                    "label": "B",
                                    "value": 16

                                }
                            ]
                        },


                    ];
                    $scope.industrySegmentData = [
                        {
                            "key": "No. of Orders",
                            "color": "#d62728",
                            "values": [
                                {
                                    "label": "Pharma",
                                    "value": -22
                                },
                                {
                                    "label": "Automotive",
                                    "value": -54
                                },
                                {
                                    "label": "Food",
                                    "value": -5
                                }
                            ]
                        },
                        {
                            "key": "Revenue Generated",
                            "color": "#1f77b4",
                            "values": [
                                {
                                    "label": "Pharma",
                                    "value": 47

                                },
                                {
                                    "label": "Automotive",
                                    "value": 53

                                },
                                {
                                    "label": "Food",
                                    "value": 16

                                }
                            ]
                        }

                    ];
                    $scope.salesEngineerData = [
                        {
                            "key": "No. of Orders",
                            "color": "#d62728",
                            "values": [
                                {
                                    "label": "Amar",
                                    "value": -22
                                },
                                {
                                    "label": "Akbar",
                                    "value": -54
                                },
                                {
                                    "label": "Anthony",
                                    "value": -5
                                }
                            ]
                        },
                        {
                            "key": "Revenue Generated",
                            "color": "#1f77b4",
                            "values": [
                                {
                                    "label": "Amar",
                                    "value": 47

                                },
                                {
                                    "label": "Akbar",
                                    "value": 53

                                },
                                {
                                    "label": "Anthony",
                                    "value": 16

                                }
                            ]
                        }

                    ];
                    $scope.customerTypeData = [
                        {
                            "key": "No. of Orders",
                            "color": "#d62728",
                            "values": [
                                {
                                    "label": "EC",
                                    "value": -22
                                },
                                {
                                    "label": "OEM",
                                    "value": -54
                                },
                                {
                                    "label": "DMC",
                                    "value": -5
                                }
                            ]
                        },
                        {
                            "key": "Revenue Generated",
                            "color": "#1f77b4",
                            "values": [
                                {
                                    "label": "EC",
                                    "value": 47

                                },
                                {
                                    "label": "OEM",
                                    "value": 53

                                },
                                {
                                    "label": "DMC",
                                    "value": 16

                                }
                            ]
                        }

                    ];
                    $scope.changeData = function (type) {
                        switch (type) {
                            case "Monthly":
                                $scope.data = [];
                                $scope.data = $scope.monthlyData;
                                break;
                            case "Customer":
                                $scope.data = [];
                                $scope.data = $scope.customerData;
                                break;
                            case "Vendor":
                                $scope.data = [];
                                $scope.data = $scope.vendorData;
                                break;
                            case "IndustrySegment":
                                $scope.data = [];
                                $scope.data = $scope.industrySegmentData;
                                break;
                            case "SalesEngineer":
                                $scope.data = [];
                                $scope.data = $scope.salesEngineerData;
                                break;
                            case "CustomerType":
                                $scope.data = [];
                                $scope.data = $scope.customerTypeData;
                                break;
                            default:
                                $scope.data = [];

                        }
                    };

                    $scope.options = {
                        chart: {
                            type: 'multiBarHorizontalChart',
                            height: 350,
                            x: function (d) {
                                return d.label;
                            },
                            y: function (d) {
                                return d.value;
                            },
                            //yErr: function(d){ return [-Math.abs(d.value * Math.random() * 0.3), Math.abs(d.value * Math.random() * 0.3)] },
                            showControls: false,
                            showValues: true,
                            duration: "500",
                            stacked: true,
                            grouped: false,
                            xAxis: {
                                showMaxMin: false
                            },

                            yAxis: {
                                axisLabel: 'Values',
                                tickFormat: function (d) {
                                    return d3.format(',f')(Math.abs(d));
                                }

                            },
                            valueFormat: d3.format(".0f"),
                            showYAxis: true,
                            showXAxis: false


                        }

                    };
                });
            </script>
            </body>
            </html>