首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Brython相当于`.bind(this)`

Brython相当于`.bind(this)`
EN

Stack Overflow用户
提问于 2021-10-18 05:15:35
回答 1查看 40关注 0票数 1

在Javascript中,在传递函数之前,通常需要通过传递调用.bind(this)来为函数提供上下文。

我很欣赏在Brython中的工作方式有点不同。但我想知道幕后是否有什么东西提供了一个可访问的this类型上下文?

具体地说,我希望将以下cell.js示例转换为Brython

代码语言:javascript
复制
<html>
<script src="https://cdn.jsdelivr.net/gh/lesichkovm/cell@1.5.0/cell.js"></script>
<script>
var el = {
  $cell: true,
  style: "font-family: Helvetica; font-size: 14px;",
  $components: [
    {
      $type: "input",
      type: "text",
      placeholder: "Type something and press enter",
      style: "width: 100%; outline:none; padding: 5px;",
      $init: function(e) { this.focus() },
      onkeyup: function(e) {
        if (e.keyCode === 13) {
          document.querySelector("#list")._add(this.value);
          this.value = "";
        }
      }
    },
    {
      $type: "ol",
      id: "list",
      _items: [],
      $components: [],
      _add: function(val) { this._items.push(val) },
      $update: function() {
        this.$components = this._items.map(function(item) {
          return { $type: "li", $text: item }
        })
      }
    }
  ]
}
</script>
</html>

在被调用之前,每个函数都是bound to the relevant element

以下是Brython代码,但正如您所看到的,没有'this‘的等价物。

代码语言:javascript
复制
<html>
<body onload="brython()">
    <script src="https://cdn.jsdelivr.net/gh/lesichkovm/cell@1.9.0/cell.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.10.0/brython.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.10.0/brython_stdlib.js"></script>

    <script type="text/python">
        from browser import window, document

        def init():
            print('init')
            #this.focus()
        def onkeyup(e):
            print('onkeyup')
            #if e.keyCode == 13:
            #    document["list"]._add(this.value);
            #    this.value = "";
        def add(val):
            print('add')
            #this._items.append(val)
        def update():
            print('update')
            #this.$components = map(lambda item: {'$item': 'li', '$text': item}, this._items)

        window.dispatchEvent(window.CustomEvent.new('cell-render', {'detail': {
            '$cell': True,
            'style': "font-family: Helvetica; font-size: 14px;",
            '$components': [{
                '$type': "input",
                'type': "text",
                'placeholder': "Type something and press enter",
                'style': "width: 100%; outline:none; padding: 5px;",
                '$init': init,
                'onkeyup': onkeyup,
            }, {
                '$type': "ol",
                'id': "list",
                '_items': [],
                '$components': [],
                '_add': add,
                '$update': update,
            }]
        }}))
    </script>
</body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2021-10-18 05:18:01

看起来我刚刚在documentation中找到了答案。

代码语言:javascript
复制
javascript.this()

Returns the Brython object matching the value of the Javascript object this. It may be useful when using Javascript frameworks, eg when a callback function uses the value of this.
The module also allows using objects defined by the Javascript language. Please refer to the documentation of these objects.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69611097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档