Javascript viewModel使用knockout.js在原型上计算函数

Javascript viewModel使用knockout.js在原型上计算函数,javascript,mvvm,knockout.js,prototype,computed-observable,Javascript,Mvvm,Knockout.js,Prototype,Computed Observable,我正在使用knockout.js库,它有助于数据绑定。所以我不断得到一个错误,我的变量没有在我们viewModel原型上的计算函数中定义。我知道这是因为计算函数正在将“this”的上下文更改为窗口,但我似乎不知道如何将其更改回根(viewModel)。我所指的方法是javascript中的“Messages”。也就是说,如何将上下文更改回viewModel 这是我的密码: HTML JAVASCRIPT var message = (function(){ function Message(

我正在使用knockout.js库,它有助于数据绑定。所以我不断得到一个错误,我的变量没有在我们viewModel原型上的计算函数中定义。我知道这是因为计算函数正在将“this”的上下文更改为窗口,但我似乎不知道如何将其更改回根(viewModel)。我所指的方法是javascript中的“Messages”。也就是说,如何将上下文更改回viewModel

这是我的密码:

HTML

JAVASCRIPT

var message = (function(){
  function Message(){
   this.main = ko.observable(true);
   this.welcome = "Welcome to Tic-Tac-Toe! This is a 2 player game. Click New Game to play!"
   this.turn = ", its your turn."
   this.win = ", you won!"
   this.draw = "It's a draw..."
  }
  return Message;
})()

var players = (function(){
  function Players(){
    this.player1 = ko.observable(true);
    this.player2 = ko.observable(false);
  }
  return Players;
})()

var aBox = (function(){
  function ABox(){
    this.symbol = ko.observable(" ")
  }

  return ABox;
})()

var viewModel = (function(){
  function ViewModel(){
    this.GameMessage = new message();
    this.thePlayers = new players();
    this.r1c1 = new aBox();
    this.r1c2 = new aBox();
    this.r1c3 = new aBox();
    this.r2c1 = new aBox();
    this.r2c2 = new aBox();
    this.r2c3 = new aBox();
    this.r3c1 = new aBox();
    this.r3c2 = new aBox();
    this.r3c3 = new aBox();

  }

/**************************************** 
 ************* Messages *****************
 ****************************************/ 

  ViewModel.prototype.StartMessage = function(){

     this.GameMessage.main(false)
  }

  ViewModel.prototype.Messages = ko.computed(function(){

    if(this.GameMessage.main()){
      return this.GameMessage.welcome;
    }
    else if(this.thePlayers.player1()){
      this.thePlayers.player1(false);
      this.thePlayers.player2(true);
      return "Player 1"+this.GameMessage.turn;

    }
    else if(this.thePlayers.player2())
      this.thePlayers.player1(true);
      this.thePlayers.player2(false);
      return "Player 2"+this.GameMessage.turn;
  },ViewModel)

  return ViewModel;
})()

ko.applyBindings(new viewModel())
我已经尝试过将上下文更改为“viewModel”,如图所示,$root和“this”

如果您想知道该方法试图实现什么,当单击“新建消息”按钮时,它将触发显示一条消息。然后,如果单击
,它将在上一条消息的位置显示另一条消息。

函数包含第二个参数,该参数为。(它只是在幕后使用)

因此,当您在原型中定义computed时,只需将computed绑定到
this
,如下所示:

ViewModel.prototype.Messages = ko.computed(function(){
  // your function code
  }, this);

请记住,当您使用原型时,
指的是您感兴趣的对象实例。

谢谢我添加了它,我使用knockout.js进行数据绑定。您在原型中使用
消息
有什么原因吗?我认为,由于这将是一个只读的,您应该能够使消息成为对象本身的一个属性。或者在访问计算结果的原型上创建一个
getMessages
。因此,我在原型上使用它的原因是因为我不想占用更多的空间。我还听说这样做是最好的做法。由于这个原因,我不想在对象本身上有任何函数。有没有办法做到这一点?我不确定是否要将它们添加到原型中,但我想说的是,如果您要这样做,并且希望有正确的上下文-从
消息中取出
ko.computed
并将其添加到构造函数中:
ko.computed(ViewModel.prototype.messages,这个)
类似的东西。是的,很好的一点,尝试那个代码并不太有效。检查一下,除了执行
ko.computed(this.messages,this)
ViewModel.prototype.Messages = ko.computed(function(){
  // your function code
  }, this);