为什么在initComponent中使用Ext.apply [英] Why use Ext.apply in initComponent

查看:159
本文介绍了为什么在initComponent中使用Ext.apply的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

initComponent 方法中的组件上设置属性时,很多代码示例使用 Ext.apply

A lot of code examples use Ext.apply when setting properties on a component in the initComponent method.

示例:

    initComponent: function(){
        Ext.apply(this, {
            items: {
                xtype: 'button'
            }
    })},

我的问题是,这样做有什么区别,相比之下这样做:

My question is, what is the difference in doing it that way, compared to doing it this way :

    initComponent: function(){
        this.items = {
            xtype: 'button'
        }
    }

对我来说,似乎更可读的方式。但是我错过了从 Ext.apply 获得的东西?

For me it seems more readable that way. But am I missing something that I get from Ext.apply?

推荐答案

Ext.apply()用于简化从源到目标对象的许多属性的复制(大部分时间源和目标对象具有不同的属性集),并且还可以用于应用默认值(第三个参数)。

Ext.apply() is used to simplify the copying of many properties from a source to a target object (most of the time the source and target objects have different sets of properties) and it can in addition be used to apply default values (third argument).

请注意,它不会产生深刻的克隆!含义如果你有一个数组或一个对象作为属性值,它将应用引用!

Note that it will not make deep clones! Meaning if you have a array or a object as property value it will apply the reference!

还有一个 applyIf(),它只复制目标对象中不存在的属性。在某些情况下,两种实现方式也具有删除复制对象的引用的好处。

There is also a applyIf() which only copies properties that do not already exist in the target object. In some cases both implementations have also the benefit of dropping the reference of the copied object.

注意:
您的第二种方式将无法正常工作,因为您缺少

这篇关于为什么在initComponent中使用Ext.apply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆