javascript - ES6中函数问题
问题描述
var ccas = 12;function ff() { var a; console.log(arguments.length);//0 console.log(arguments[0]);//undefine; console.log(arguments.length > 0);//false console.log(a = arguments.length > 0 && 1);//false console.log(arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ccas);//12 var y = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ccas;//12 return function () {console.log(arguments.length);var ccas = 7;console.log(y); }.apply(null, arguments);}ff();为什么y的值是12,求大神告知
问题解答
回答1:var y = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ccas;//12 这是一个三目运算,arguments.length > 0 =》 falsearguments[0] !== undefined =》 false所以就成了=》var y = false && false ? arguments[0] : ccas;=》var y = false ? arguments[0] : ccas;不知道你是不理解三目运算还是?值为true取值 : 前面的,false取值后面的。所以y=12;回答2:
因为 arguments[0]==undefined且 var y = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ccas;//12所以 y=12
如果你想问为什么不等于7你需要学习一下闭包和基本类型和引用类型
参考链接:http://www.cnblogs.com/chengg...http://www.ruanyifeng.com/blo...
回答3:var y = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ccas;//12
因为这里y就是12
相关文章:
1. MYSQL新建用户设置可以远程访问的问题2. python - 求一个在def中可以实现调用本def满足特定条件continue效果的方法(标题说不太清楚,请见题内描述)3. golang - 用IDE看docker源码时的小问题4. mysql - SQL操作时间的函数?5. docker网络端口映射,没有方便点的操作方法么?6. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””7. 正则表达式 - python pandas的sep参数问题8. javascript - ionic run android报错9. javascript - 用表单提交两个时间段请求后台返回对应数据时出现的一些问题!10. docker内创建jenkins访问另一个容器下的服务器问题
