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. 452. Remove Linked List Elements【Naive】

    Remove all elements from a linked list of integers that have value val. Example Given 1->2->3- ...

  2. 从“空项目”创建MFC项目遇到的问题error C1189,error MSB8031

    在VS2013中创建了一个空项目,创建了MyApp.h, MyApp.cpp(MyApp.h使用了<afxwin.h>) build的时候报错: fatal error C1189: #e ...

  3. linux 驱动cc1101

    cc110x.h /*  * File:   cc110x.h * Author: elinux * * Created on 2015年4月7日, 上午10:32 */ #ifndef CC110X ...

  4. repcached与mysql缓存測试

    使用gem安装mysql引擎 gem install mysql(假设安装失败.请查找一些依赖组建是否安装,比如mysql-devel) 编写ruby脚本,先获取mysql数据,之后从memcache ...

  5. tomcat上

    1. Tomcat简介 Tomcat是一个web服务器 web服务器:httpd,nginx web 处理静态文件:html css.js.jpg,png Tomcat 处理 html文件 php软件 ...

  6. Spring.Net框架三:使用Spring.Net框架实现多数据库

    在前面的两篇文章中简单介绍了Spring.Net和如何搭建Spring.Net的环境,在本篇文章中将使用Spring.Net实现多数据库的切换. 一.建立一个空白的解决方案,名称为“SpringDot ...

  7. MyBatis Generator 学习

    根据数据库,自动生成 VO.XML或者DAO的工具. 同大多数工具(或者框架)一样,需要加载一个配置文件,然后根据配置文件中的内容连接数据库,访问其中的表内容,最后生成实体类以及MAPPER. 占位用 ...

  8. 多媒体开发之rtsp---rtsp client 端的实现

    http://blog.csdn.net/xyz_lmn/article/details/6055179 java实现 http://www.cnblogs.com/wohexiaocai/p/454 ...

  9. 【UVa】Jump(dp)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. zara

    [1]ZARA是西班牙Inditex集团旗下的一个子公司,它既是服装品牌,也是专营ZARA品牌服装的连锁零售品牌.1975年设立于西班牙的ZARA,隶属于Inditex集团,为全球排名第三.西班牙排名 ...