首页 > 解决方案 > 我在 ubuntu 中学习 js,但是当我给出节点 index.js 的 cmd 时,它将进入新行并忽略第一个 cmd

问题描述

var user = {
    firstName: "vishu",
    courseCount: 10,
    getCourseCount: function () {
        console.log("line 7",this);
        function sayHello(){
            console.log("hello");
            console.log("line 10", this);
        }
        sayHello();
    }
}

标签: javascriptnode.js

解决方案


你希望命令做什么?如果您希望它运行您的脚本,它可以。它没有显示任何内容,只是因为您的 js 脚本没有调用任何内容,只是构建了一个对象。

要让它调用您的函数,请尝试

var user = {
    firstName: "vishu",
    courseCount: 10,
    getCourseCount: function () {
        console.log("line 7",this);
        function sayHello(){
            console.log("hello");
            console.log("line 10", this);
        }
        sayHello();
    }
}
user.getCourseCount() // add this line to call the function in your user object

推荐阅读