javascript Ember.js 在哪里调用 this._super()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10852988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:15:31  来源:igfitidea点击:

Ember.js where to call this._super()

javascriptember.jsoverriding

提问by Ilia Choly

I've been going through the Ember documentation and am seeing an inconsistency in where the _supermethod is being called when overriding init.

我一直在浏览 Ember 文档,发现_super在覆盖init.

This is the most common and is what I've been using so far

这是最常见的,也是我迄今为止一直在使用的

var Foo = Em.Object.extend({
    init: function(){
        this._super();
        // ... my stuff ...
    }
});

last night I was reading through this write upand saw an example doing this

昨晚我正在阅读这篇文章,看到了一个这样做的例子

var Bar = Em.Object.extend({
    init: function(){
        // ... my stuff ...
        return this._super();
    }
});

It was actually an Ember.ContainerViewin the code snippet.

它实际上是Ember.ContainerView代码片段中的一个。

Can anyone explain this? My code OCD is acting up and I can't move on until I know.

谁能解释一下?我的代码强迫症正在起作用,在我知道之前我无法继续前进。

采纳答案by hvgotcodes

In the documentation linked

在链接的文档中

 init: function() {
    var childViews = this.get('childViews');
    var descriptionView = App.DescriptionView.create();
    childViews.pushObject(descriptionView);
    this.addButton();
    return this._super();
  },

_super()is called AFTER the descriptionView is created and pushed onto the childViewsarray.

_super()在创建 descriptionView 并将其推送到childViews数组后调用。

That's because the superclass initimplementation is going to take the childViews array and do stuff with it. If you called _superbefore adding the descriptionViewto the array, it wouldn't get processed by whatever initdoes....

那是因为超类init实现将采用 childViews 数组并用它做一些事情。如果你_super在添加descriptionView到数组之前调用它,它不会被任何东西init处理......

I'm inferring, but that's the way it works in Sproutcore, from which Ember derives, so I think it's probably the same.

我是在推断,但这就是它在 Sproutcore 中的工作方式,Ember 源自它,所以我认为它可能是相同的。