w作用域控制变量的可见范围。

JavaScript: The Good Parts

Instead of initializing myObject with an object literal, we will initialize myObject by calling a function that returns an object literal. That function defines a value variable. That variable is always available to the increment and getValue methods, but the function's scope keeps it hidden from the rest of the program

 <script type="text/javascript">
var wObject = function () {
var value = 0;
return {
increment: function (inc) {
value += typeof inc === 'number' ? inc : 1;
},
getValue: function () {
return value;
}
}
}();
wf(wObject)
function wf(w) {
console.log(w)
} wf(wObject.increment(3))
wf(wObject.getValue())
wf(wObject.increment(3))
wf(wObject.getValue()) var wObject = function () {
var value = 0;
return {
increment: function (inc) {
value += typeof inc === 'number' ? inc : 1;
return 'www';
},
getValue: function () {
return value;
}
}
}();
wf(wObject)
function wf(w) {
console.log(w)
} wf(wObject.increment(3))
wf(wObject.getValue())
wf(wObject.increment(3))
wf(wObject.getValue())
</script>

initialize myObject by calling a function that returns an object literal的更多相关文章

  1. 关于Function原型对象和Object原型对象的一些疑惑

    网上有一道美团外卖的面试题是这样的: Function.prototype.a = 'a'; Object.prototype.b = 'b'; function Person(){}; var p ...

  2. Calling Matlab function from python: “initializer must be a rectangular nested sequence”

    I am writing a python script from which I hope to call the Matlab anovan function. I have attempted ...

  3. 解决方法:loadrunner 场景下执行webservice脚本是---报错10492 Error: Exception was raised when calling per-process-init function in extens

    在vug下执行时,脚本无异常,但是在controller下执行时报下面错误,网上查了下,解决方法千奇百怪,但无一可行. 分析了下错误,似乎是初始化进程有关.想到rts中的设置习惯时以线程方式执行. 遂 ...

  4. Know How To Use ID_NULL Function To Search An Object In Oracle Forms

    ID_NULL built in function is used to determine that an object type variable is null or not null in O ...

  5. express框架路由未导出错误:Router.use() requires a middleware function but got a Object

    在路由的文件下加入导出语句 module.exports = router   导入与导出需要一一对应  

  6. JavaScript- The Good Parts Chapter 4

    Why, every fault’s condemn’d ere it be done:Mine were the very cipher of a function. . .—William Sha ...

  7. 编写高质量js代码

    原文链接:http://code.tutsplus.com/tutorials/24-javascript-best-practices-for-beginners--net-5399 jquery代 ...

  8. JavaScript- The Good Parts Chapter 5 Inheritance

    Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but ...

  9. React源码学习——ReactClass

    前言 之前一直在使用react做开发,但是对其内部的工作机制却一点儿都不了解,说白了就是一直在套api,毫无成就感.趁最近比较闲,对源码做了一番研究,并通过博客的方式做一些记录. 进入正题 通过编写自 ...

随机推荐

  1. WIN XP 命令汇总

    winver检查Windows版本dxdiag检查DirectX信息mem.exe显示内存使用情况Sndvol32音量控制程序sfc.exe系统文件检查器gpedit.msc 组策略regedit.e ...

  2. IIS添加域名

    前提:域名可用 1.打开网站,点击右侧 绑定 2.添加域名  点击确定 3.结果: ok  配置完成.

  3. CentOS下安装网卡驱动

    前言最近,一台机器从FreeBSD换为CentOS,一路安装顺利.完事之后发现,网卡没有启用,dmesg | grep eth 命令确定网卡没有驱动导致的.于是开始了一路艰辛的安装过程. 安装过程1. ...

  4. xslt 映射 xml

    1.xslt文件映射xml文件中的A节点的时候,如果A节点有属性的话,先把属性值映射出来,然后再映射节点的值,如下: xml文件: <A age="11" sex=" ...

  5. mysql 位运算

    & 与运算 |  或运算 ^ 异或运算 或者 你也可以将 与运算理解为 + 法  例如 1|2 = 3   (1+2 = 3)1|2|4 = 7 (1+2+4 = 7) 将 异或运算理解为 - ...

  6. 16V554 的测试代码

    //------------------------------------------------------------------------ #include   "AT16C554 ...

  7. ajax——优化0126(增删改查:添加查看详情,返回结果类型为JSON型,在窗口显示)

    效果: 鼠标点击查看详情时 数据库: 0126.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...

  8. Android基础总结(十一)Fragment,动画

    Fragment(重要) 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容 生命周期方法跟Activity一致,可以理解把其为就是一个Activity fragmen ...

  9. Oracle 错误:ORA-06413: Connection not open 解决办法

    http://blog.csdn.net/neso520/article/details/6037411 ——————————————————————————————————————————————— ...

  10. jqgrid demo

    本人是用php写的,相信只要稍微用点时间看本人写的,就一定能看懂 前台代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//E ...