Uncaught ReferenceError: useStudent is not defined

Uncaught ReferenceError: useStudent is not defined

我知道这可能是个愚蠢的问题,我可能没有以正确的方式问这个问题,但我一直停留在这一部分我一直在尝试 运行 这个程序,它说“未捕获的 ReferenceError:useStudent 是not defined” 当我在“Student”上检查控制台时,我检查了我的脚本,它非常好,我的代码

 
<script src="A2.js"></script>
<body>
  
  <div class="column1">
    <div class="input">
      
      <button onclick="useStudent()"> Student</button>
      <button onclick="useCar()"> Car</button>

      
    
</body>
</html>
here is my script added to this code
function useStudent(){
  var stu = "Jon Lee";
  
  var ye = 3;   
  console.log("year: " + ye);
  
  var maj = "Math"  
  console.log("major: " + maj)
  
  var message = stu.displayMe();
  console.log(message);
  
  console.log('-------------');
  
  // set year to be 4
  ye = 4;
  // set major to be "test"
  maj = "test";
  
  // output year and major
  console.log("year: " + ye);
  console.log("major: " + maj);
  
  message = stu.displayMe();
  console.log(message);
  
 }

here is displayme
displayMe() {
      this.id + this.name + this.Year + this.Major
  }

你定义的“stu”类型是一个字符串,看起来你需要一个student对象。因此,您需要一个构造函数来创建学生对象并将 displayMe() 方法设置为其 属性.

function Stu (id,name, year,maj){
    this.id = id;
    this.name = name;
    this.year = year;
    this.major = maj;
    this.displayMe = function(){ return  this.id + this.name + this.year + this.major}
}

function useStudent(){
    let stu = new Stu(1,"Jon Lee",3,'Math')
    console.log("year: " + stu.year);
    console.log("major: " + stu.maj);
    console.log(stu.displayMe() );
    console.log('-------------');

    stu.maj = 'EECS';
    stu.year = 4;

    console.log("year: " + stu.year);
    console.log("major: " + stu.maj);
    console.log(stu.displayMe() );
    console.log('-------------');
}