es6方法内外构造函数 - knockout

时间:2017-03-08 23:28:06

标签: javascript knockout.js ecmascript-6

在es6中给出以下类:

class Test {
  constructor() {
    this.foo = () => console.log('Hello World');
  }

  bar() {
    console.log('Hello World')
  }
}

foo和bar有什么区别?我可以通过这个调用(this.foo,this.bar)。但是,如果我在knockoutJS中使用此语法,则Knockout无法找到bar()。

class Test2 {
  constructor() {
    this.foo = () => this.foo(); // endless loop
  }

  foo() {
    console.log('Hello World')
  }
}

我不知道这是否合理,但我想在Test2中看到你能看到的内容。主要是,我想将覆盖功能纳入我的淘汰应用程序。如果我从Test1延伸并覆盖foo,我将无法拨打super.foo()

我的目标是启用类继承,同时允许覆盖专门分配给'this'的函数(例如attach / dispose)。

编辑:作为解决方法,我可以像:

一样处理
class Test3 {
  constructor() {
    this.foo = () => _foo();
  }

  _foo() {
    console.log('Hello World')
  }
}

这将允许我在淘汰赛中使用foo并仍然能够使用_foo覆盖它。

1 个答案:

答案 0 :(得分:1)

  

foo和bar有什么区别?

请参阅Use of 'prototype' vs. 'this' in JavaScript?

  

如果我在knockoutJS中使用此语法,则Knockout无法找到bar()

看起来Knockout与语言开发不是最新的。在# configure tags for all your text windows foreach t [ getAllTextWindowPaths ] { $t tag configure selected -background white -foreground black } # configure the column select button button .canvas.button.column1 -text {1} -command [ list clickColumn .canvas.button.column1 1 ] proc selectText { textpath { select 1 } } { if { $select == 1 } { $textpath tag add 1.0 end selected } else { $textpath tag remove 1.0 end selected } } proc clickColumn { colpath colnum } { # now call this click handler for for your column of textwidgets # you can make same for a row foreach t [getTextPathsInColumn $colnum ] { selectText $t 1 } $colpath configure -command [list unclickColumn $colpath $colnum ] } proc unclickColumn { colpath colnum } { # now call this click handler for for your column of textwidgets # you can make same for a row foreach t [getTextPathsInColumn $colnum ] { selectText $t 1 } $colpath configure -command [list clickColumn $colpath $colnum ] } proc getSelectedContents { } { # im adding to a list all the seleted contents but you can arrange # it anyway you want set retval {} foreach t [getAllTextWindowPaths ] { if { "selected" in [$t tag names 1.0 ] } { lappend retval [ $t get 1.0 end ] } } # optional call selectText $t 0 here foreach selected widget # to clear selection or handle the column click handler again return $retval } es上定义的方法是不可枚举的。有关如何访问它们,请参阅ES6 Iterate over class methods

  

我的目标是启用类继承,同时允许覆盖专门分配给'这个' (例如附加/处置)。

您始终可以class

相关问题