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

查看:16
本文介绍了为什么在 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.

注意:您的第二种方法将不起作用,因为您缺少 this.

Note: Your second way won't work because you are missing this.

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

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