请参阅这些文章以获取"Ext.applyIf"函数和双管道的参考。
http://docs.sencha.com/core/manual/content/utility.html
http://homepage.ntlworld.com/kayseycarvey/controlflow4.html
有人可以解释一下这个逻辑在 ExtJS 框架中做什么吗? 我想确保我的解释在管道的第一行是正确的(尤其是)。
var params = Ext.applyIf(operation.params || {}, this.extraParams || {}), request;
params = Ext.applyIf(params, this.getParams(params, operation));
if (operation.id && !params.id) {
params.id = operation.id;
}
取自 ASP.NET asmx 自定义服务器代理类:
Ext.define('Ext.ux.AspWebAjaxProxy', {
extend: 'Ext.data.proxy.Ajax',
require: 'Ext.data',
buildRequest: function (operation) {
var params = Ext.applyIf(operation.params || {}, this.extraParams || {}), request;
params = Ext.applyIf(params, this.getParams(params, operation));
if (operation.id && !params.id) {
params.id = operation.id;
}
params = Ext.JSON.encode(params);
request = Ext.create('Ext.data.Request', {
params: params,
action: operation.action,
records: operation.records,
operation: operation,
url: operation.url
});
request.url = this.buildUrl(request);
operation.request = request;
return request;
}
});
- 双管道是指定默认值的 JavaScript 技巧。就像 Evan 建议的那样,它经常被用来避免空指针问题。在
var a = b||{}
的示例中,即使 b 为空或未定义,a 也保证不为空。默认回退使a = {}
(空对象)。 -
ApplyIf
方法将属性从源复制到目标,注意不要覆盖任何现有属性。在下面的示例中,只要参数未定义这些属性,extraParams 属性就会添加到参数属性中。Ext.applyIf(params , extraParams )
实际代码为:
Ext.applyIf(operation.params || {}, this.extraParams || {}),
这将额外的参数添加到参数对象中,小心避免任何空指针问题,并确保不要覆盖参数。
考虑:
function foo(cfg) {
cfg = cfg || {};
console.log(cfg.x);
}
foo({x: 1});
foo();
所以本质上,我们的意思是,如果传递给方法的 cfg 是"falsy"(读取、未定义或 null),则将其设置为空对象,以便在读取"x"属性时不会收到引用错误。