节点js需要执行功能

时间:2018-06-23 22:17:22

标签: javascript node.js

我无法执行该函数。在终端中运行node app.js时,我得到[Function: hi]

我有两个文件

app.js

var show = require('./code');
console.log(show.hi);

code.js

function hi() {
    console.log('I am inside function Hi');
}

module.exports.hi = hi;

如果我会做这样的事情console.log(show.hi()),那么我会得到

I am inside function Hi
undefined

2 个答案:

答案 0 :(得分:1)

因为您没有调用该函数。

称呼它

show.hi();

由于hi函数未返回任何内容,因此您变得不确定。删除console.log我的意思是只执行功能show.hi()

简而言之console.log(show.hi);应该是show.hi()

答案 1 :(得分:1)

如果要执行功能,请执行以下操作:

<table class="grammar table table-hover" data-toggle="table" data-sort-name="instance_use" data-sort-order="desc">
            <thead><tr><th style="" data-field="grammar_type" tabindex="0"><div class="th-inner sortable both">Grammar Type</div><div class="fht-cell"></div></th><th style="" data-field="instance_use" tabindex="0"><div class="th-inner sortable both desc">Instances of Use</div><div class="fht-cell"></div></th></tr></thead>
            <tbody><tr data-index="0"><td style=""><span class="adverb" onclick="toggleClass(this)">Adverb</span></td><td style="">2 </td></tr><tr data-index="1"><td style=""><span class="verb-present" onclick="toggleClass(this)">Verb, present</span></td><td style="">2 </td></tr><tr data-index="2"><td style=""><span class="determiner" onclick="toggleClass(this)">Determiner</span></td><td style="">2 </td></tr><tr data-index="3"><td style=""><span class="noun-sing-or-mass" onclick="toggleClass(this)">Noun, sing. or mass</span></td><td style="">1 </td></tr><tr data-index="4"><td style=""><span class="" onclick="toggleClass(this)">Preposition</span></td><td style="">1 </td></tr><tr data-index="5"><td style=""><span class="noun-plural" onclick="toggleClass(this)">Noun, plural</span></td><td style="">1 </td></tr><tr data-index="6"><td style=""><span class="comma" onclick="toggleClass(this)">Comma</span></td><td style="">1 </td></tr><tr data-index="7"><td style=""><span class="personal-pronoun" onclick="toggleClass(this)">Personal pronoun</span></td><td style="">1 </td></tr></tbody>
        </table>
        
<div id="story_text">
            <span style="white-space: pre-line">
            <span class="adverb">here</span> <span class="verb-present">is</span> <span class="determiner">a</span> <span class="noun-sing-or-mass">story</span> <span class="">with</span> <span class="determiner">no</span> <span class="noun-plural">commas</span><span class="comma">,</span> <span class="adverb">now</span> <span class="personal-pronoun">it</span> <span class="verb-present">does</span>
            </span>
        </div>

var show = require('./code'); // get the `hi` property and call it as a function show.hi(); show模块中的module.exports对象。因此,code将是您分配给导出的show.hi属性。然后,要执行该功能,必须在其后使用hi