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. JQM事件详解

    在前文<使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 默认配置与事件基础>中,Kayo 对 jQuery Mobile 事件的基 ...

  2. js生成二维码的jquery组件–qrcode

    js生成二维码的jquery组件–qrcode 2015/01/30 / 2508 VIEWS / JAVASCRIPT, JQUERY 有一些耗cpu的计算,完全可以在客户端上计算,比如生成二维码. ...

  3. 使用JSON Web Tokens和Spring实现微服务

    http://www.jdon.com/dl/best/json-web-tokens-spring-cloud-microservices.html

  4. 用广搜实现的spfa

    用广搜实现的spfa,如果是用一般的最短路,会发现构图很麻烦,因为它不是路径带权值,而是自身带权值.写起来只要注意,在点出队列的生活将其标记为0,在要压入队列的时候,判断其标记是否为0,为0表示队列中 ...

  5. vuex 开始

    每一个vuex的应用的核心都是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态(state),vuex和单纯的全局对象有以下两点不同: 1,vuex的状态存储是响应式的 ...

  6. 对java中arraylist深入理解

    1.ArrayList插入删除一定慢么? 取决于你删除的元素离数组末端有多远,ArrayList拿来作为堆栈来用还是挺合适的,push和pop操作完全不涉及数据移动操作. 2.ArrayList的遍历 ...

  7. nodejs具体解释

    文件夹 javascript与node.js     javascript与你     因为javascript真正意义上有两种,甚至能够说是三种形态(从最早的作为DHTML进行增强的小工具,到像jQ ...

  8. Android——Activity初学

    manifests里的AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> < ...

  9. Hdu 2236 无题II 最大匹配+二分

    题目链接: pid=2236">Hdu 2236 解题思路: 将行和列理解为二分图两边的端点,给出的矩阵即为二分图中的全部边, 假设二分图能全然匹配,则说明 不同行 不同列的n个元素 ...

  10. js实现EasyUI-datagrid前台分页

    //实现假分页 function myLoader(param, success, error) { var that = $(this); var opts = that.datagrid(&quo ...