如何用node执行js代码
以前没怎么写过前端代码,一直以为,写前端代码需要在浏览器里面执行,打开控制台,然后debug之类的。
首先写一段js的代码
// 类
class Animal{
constructor(name) {
this.name = name
}
run() {
return `${this.name} is running`
}
}
const snake = new Animal('lily')
console.log(snake.run())
class Dog extends Animal{
bark(){
return `${this.name} is barking`
}
}
const xiaobao = new Dog('xiaobao')
console.log(xiaobao.run())
console.log(xiaobao.bark())
进入cmd窗口,执行命令node class.js
c>node class.js
lily is running
xiaobao is running
xiaobao is barking
本文介绍了如何在命令行中使用Node.js运行JavaScript代码。通过创建Animal类和Dog类的示例,展示了类的继承和方法调用。在CMD中执行`node class.js`成功打印了预期的运行结果。
1393

被折叠的 条评论
为什么被折叠?



