function(config) {var config = config || {};}这种写法是什么意思

chenleiwei 2010-12-15 08:16:41
App.BookStore = function(config) {
var config = config || {}; //我很想知道这种写法是什么意思
Ext.applyIf(config, {
reader: new Ext.data.XmlReader({
// records will have an "Item" tag
record: 'Item',
id: 'ASIN',
totalRecords: '@total'
}, [
// set up the fields mapping into the xml doc
// The first needs mapping, the others are very basic
{name: 'Author', mapping: 'ItemAttributes > Author'},
'Title',
'Manufacturer',
'ProductGroup',
// Detail URL is not part of the column model of the grid
'DetailPageURL'
])
});
// call the superclass's constructor
App.BookStore.superclass.constructor.call(this, config);
};
...全文
913 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
licip 2010-12-16
  • 打赏
  • 举报
回复
(function(){省略中间代码部分……}).() 这种写法让你写的这个函数直接执行
hch126163 2010-12-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 gaojian001 的回复:]

JScript code

var config = config || {}; //我很想知道这种写法是什么意思


中间的||是或的意思,这样的写法等同于 if !config {config={}}
即取这两个值中不为空的那一个(准确点说是为真的那一个,因为在js里,空值代表false,有值的变量在判断时可以做为true)
[/Quote]

正解

|| 运算符特性,高效!

当config= 0 null false undefined
时,设置为一个 对象 {}
wllllll 2010-12-16
  • 打赏
  • 举报
回复
var config = config || {};

|| 他是或者
你可以理解成执行顺序, 如果 || 符号之前执行结果为 0、假、null、undefined、空字符 时继续执行 || 之后的代码
直到某次执行不获得这些值,那么返回这个值


<script type="text/javascript" >
function f(v){
alert(v)
if ( v === 2 ) return true
return false
}


var a = f(1) || f(2) || f(3) || f(4)
</script>
flyerwing 2010-12-16
  • 打赏
  • 举报
回复
未配置则使用默认配置.
逍遥庄主 2010-12-16
  • 打赏
  • 举报
回复
相当于 config?config:{}
chenleiwei 2010-12-16
  • 打赏
  • 举报
回复
在一个js文件中这样写到: (function(){省略中间代码部分……}).() 这种写法是什么呢?
fxs_2008 2010-12-15
  • 打赏
  • 举报
回复
看运算符优先级
Jeffrey84 2010-12-15
  • 打赏
  • 举报
回复
防止后面引用这个对象的属性时出错
王集鹄 2010-12-15
  • 打赏
  • 举报
回复
var config = undefined;

alert(config.a); // 抛异常。因为config为undefined

安全的写法是
if (config) alert(config.a);

---------

var config = undefined;

config = config || {};
config.a // 打印undefined,这样保证config至少能访问属性,省去判断,否则每一个属性你都要判断config是否可以访问。


gaojian001 2010-12-15
  • 打赏
  • 举报
回复

var config = config || {}; //我很想知道这种写法是什么意思

中间的||是或的意思,这样的写法等同于 if !config {config={}}
即取这两个值中不为空的那一个(准确点说是为真的那一个,因为在js里,空值代表false,有值的变量在判断时可以做为true)

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧