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. 上传图片流到服务器(AFN方法) (多张图片)(图片流)

      上传图片流到服务器(AFN方法) (多张图片)(图片流) 第一步//获取图片 UIAlertController *actionSheet = [UIAlertController alertCo ...

  2. jMiniLang设计思路

    前言 项目地址:https://github.com/bajdcc/jMiniLang 演示视频:https://www.bilibili.com/video/av13294962 jMiniLang ...

  3. java 新特性学习笔记

    java 1.7 Files.write(path,list,StandardCharsets.UTF_8,StandardOpenOption.APPEND); String preTime = F ...

  4. SpringBoot使用maven构建

    1.使用maven作为parent管理 maven用户可以继承spring-boot-starter-parent项目获取合适的默认设置.该父项目提供一下特性: 默认编译级别为Java1.6 源编码格 ...

  5. phpadmin 装了6666端口只能在IE打开,在阿里云改了 开放端口85好了

    phpadmin 装了6666端口只能在IE打开,在阿里云改了 开放端口85好了 非常用端口谷歌浏览器识别不了phpadmin

  6. 03 java基本

    输入输出流 标准输出流比较简单, 只需要使用 System.out.println 就可以了, 标准输入流步骤如下: Scanner in = new Scanner(System.in); 然后可以 ...

  7. AJAX解惑篇(转)

    这篇文章会使你对AJAX有一个基本了解,并给出两个容易上手的例子. 什么是AJAX AJAX是一个新的合成术语,隐含了两个已经存在多年的JavaScript特性,但是直到最近,随着一些诸如Gmail. ...

  8. php -- 文件操作类(文件或文件夹的:创建、删除、复制、移动)

    <? /** * 操纵文件类 * * 例子: * FileUtil::createDir('a/1/2/3'); 测试建立文件夹 建一个a/1/2/3文件夹 * FileUtil::create ...

  9. add命令

    将文件加入到索引,要使用add命令.在<file>指定加入索引的文件.用空格分割可以指定多个文件. git add demo.html test.html //添加两个文件 添加当前目录下 ...

  10. thinkPHP中phpexcel的导出功能

    public function wExcel() { vendor("PHPExcel.PHPExcel"); // Create new PHPExcel object $obj ...