未捕获的ReferenceError:未在onclick上定义obj

时间:2018-10-11 03:40:06

标签: javascript

<button class="btn btn-danger" onclick="obj.two">Show Oops</button>

<button class="btn btn-success" onclick="obj.edit">Edit Items</button>

每当我单击show oops时。在控制台中,错误是未捕获的ReferenceError:HTMLButtonElement.onclick中未定义obj

我的JavaScript代码:

class User {

constructor()
    {
    this.name = "joker t-shirt";
    this.product = 200;
    this.Quantity = 4;
    this.total = this.product * this.Quantity;
    }


get two()  this is obj.two() ,but it spits out an error
   {    
   }

由于没有定义对象,我遇到了错误,请帮忙

1 个答案:

答案 0 :(得分:0)

您必须将obj定义为User()的实例:

var obj = new User();

class User {

  constructor() {
    this.name = "joker t-shirt";
    this.product = 200;
    this.Quantity = 4;
    this.total = this.product * this.Quantity;
  }

  get two() { 
    console.log('Show oops button is Working...');
  }

  get edit() {
    console.log('Edit button is Working...');
  }

}

var obj = new User();
<button class="btn btn-danger" onclick="obj.two">Show Oops</button>

<button class="btn btn-success" onclick="obj.edit">Edit Items</button>