通过 class 方法访问 javascript 中的 class 属性

Access class properties in javascript from class method

这是我第一次尝试在 javascript 中使用 类 制作 mvc 模型。

通过我的控制器对象应用程序的模型对象的 getFormValues() 方法,我想读取所有输入文件的值。然后我想将它们保存到我的控制器对象的模型对象的表单值 属性 中。这行不通!

console.log(app.model.getFormValues()); 向我展示了具有表单值的数组。

console.log(app.model.formValues); 显示了一个空数组。

如何将方法 getFormValues 中的数组存储在模型对象的 属性 formValues 中?

我的HTML:

<!doctype html>
<html lang="en">
 <head>
   <!-- Required meta tags -->
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

   <!-- Bootstrap CSS -->
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

   <title>Hello, world!</title>
 </head>
 <body>

   <form>
     <div class="row">
       <div class="col">
         <input type="text" class="form-control" placeholder="First name">
       </div>
       <div class="col">
         <input type="text" class="form-control" placeholder="Last name">
       </div>
     </div>
   </form>

   <button id="verification"> Verify</button>


   <!-- Optional JavaScript -->
   <!-- jQuery first, then Popper.js, then Bootstrap JS -->
   <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
   <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

   <script src="js.js"></script>
 </body>
</html>

我的JS/Jquery:

class Model {

  constructor() {
    this.formValues = this.getFormValues();
  }

   getFormValues() {
    let $inputs = $("input");
    var formValues = [];
    $inputs.each(function(index) {
      let value1 = $(this).val();
      formValues.push(value1);
    });
    return formValues;
   }


}

class View {
  constructor() {}
}

class Controller {
  constructor(model, view) {
    this.model = model
    this.view = view
  }
}

const app = new Controller(new Model(), new View());

check = function () {
  console.log(app.model.getFormValues());
  console.log(app.model.formValues);
}

$("#verification").on("click", check);

提前问候和感谢 法比安

您的问题是基于应用常量的创建。

您在首次加载页面时创建应用常量。 此时输入为空,应用常量也是空的。

编辑:当您点击我的代码片段中的按钮时更新您的变量。

class Model {

  constructor() {
    this.formValues = this.getFormValues();
  }

   getFormValues() {
    let $inputs = $("input");
    var formValues = [];
    $inputs.each(function(index) {
      let value1 = $(this).val();
      formValues.push(value1);
    });
    return formValues;
   }


}

class View {
  constructor() {}
}

class Controller {
  constructor(model, view) {
    this.model = model
    this.view = view
  }
}

const app = new Controller(new Model(), new View());

check = function () {
  app.model.formValues = app.model.getFormValues();
  console.log(app.model.getFormValues());
  console.log(app.model.formValues);
}

$("#verification").on("click", check);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
     <div class="row">
       <div class="col">
         <input type="text" class="form-control" placeholder="First name">
       </div>
       <div class="col">
         <input type="text" class="form-control" placeholder="Last name">
       </div>
     </div>
   </form>

   <button id="verification"> Verify</button>