【发布时间】:2020-03-22 09:37:43
【问题描述】:
我的代码
console.log('Starting app');
const fs = require('fs');
const os = require('os');
const _ = require('lodash');
const notes = require('./notes.js');
var command = process.argv[2];
console.log(command);
if (Comment = 'add') {
console.log('adding new note');
} else if (command = 'list') {
console.log('listing all notes');
} else {
console.log('command not recognized');
}
当我跑步时
node app.js list
我明白了
Starting app
Starting node.js
list
adding new note
我的 else if 语句有什么问题?
【问题讨论】:
-
Comment = 'add'不是比较,而是分配。它的值是赋值后Comment的值(即'add'),它永远不会为假。阅读JavaScript operators -
if (Comment = 'add')->if (command == 'add') -
本题不涉及Node.js。
-
好的,它来自 Node.js 书,但是 javascipt 语法。
标签: javascript node.js