fanfan_gg 2013-10-23 04:51 采纳率: 50%
浏览 2245
已采纳

关于 Extjs 4.X中 Ext.Function源码中的一些问题, 关于javascript基础知识的

Ext.Function中定义了如下一个叫做flexSetter的函数,其作用看看就明白
问题在于 其中做a===null判断的地方为什么不用a==null呢?

源码如下:

Ext.Function = {

/**
 * A very commonly used method throughout the framework. It acts as a wrapper around another method
 * which originally accepts 2 arguments for `name` and `value`.
 * The wrapped function then allows "flexible" value setting of either:
 *
 * - `name` and `value` as 2 arguments
 * - one single object argument with multiple key - value pairs
 *
 * For example:
 *
 *     var setValue = Ext.Function.flexSetter(function(name, value) {
 *         this[name] = value;
 *     });
 *
 *     // Afterwards
 *     // Setting a single name - value
 *     setValue('name1', 'value1');
 *
 *     // Settings multiple name - value pairs
 *     setValue({
 *         name1: 'value1',
 *         name2: 'value2',
 *         name3: 'value3'
 *     });
 *
 * @param {Function} setter
 * @returns {Function} flexSetter
 */
flexSetter: function(fn) {
    return function(a, b) {
        var k, i;

        if (a === null) {
            return this;
        }

        if (typeof a !== 'string') {
            for (k in a) {
                if (a.hasOwnProperty(k)) {
                    fn.call(this, k, a[k]);
                }
            }

            if (Ext.enumerables) {
                for (i = Ext.enumerables.length; i--;) {
                    k = Ext.enumerables[i];
                    if (a.hasOwnProperty(k)) {
                        fn.call(this, k, a[k]);
                    }
                }
            }
        } else {
            fn.call(this, a, b);
        }

        return this;
    };
},
  • 写回答

1条回答

  • fanfan_gg 2013-10-23 14:46
    关注

    哈哈 结贴
    作者的思路是过滤null不过滤undefined 也就是说 作者允许这样

    var myObj={
    setValue:Ext.Function.flextSetter(function(name,value){
        this[name]=value;
    })
    

    };

    myObj.setValue(null,"none");

    而不允许这样

    var myObj={
    setValue:Ext.Function.flextSetter(function(name,value){
        this[name]=value;
    })
    

    };

    myObj.setValue();

    第一种容错 第二种不容错,容错处理点到为止,不过头

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?