initialize myObject by calling a function that returns an object literal
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的更多相关文章
- 关于Function原型对象和Object原型对象的一些疑惑
网上有一道美团外卖的面试题是这样的: Function.prototype.a = 'a'; Object.prototype.b = 'b'; function Person(){}; var p ...
- 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 ...
- 解决方法:loadrunner 场景下执行webservice脚本是---报错10492 Error: Exception was raised when calling per-process-init function in extens
在vug下执行时,脚本无异常,但是在controller下执行时报下面错误,网上查了下,解决方法千奇百怪,但无一可行. 分析了下错误,似乎是初始化进程有关.想到rts中的设置习惯时以线程方式执行. 遂 ...
- 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 ...
- express框架路由未导出错误:Router.use() requires a middleware function but got a Object
在路由的文件下加入导出语句 module.exports = router 导入与导出需要一一对应
- 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 ...
- 编写高质量js代码
原文链接:http://code.tutsplus.com/tutorials/24-javascript-best-practices-for-beginners--net-5399 jquery代 ...
- JavaScript- The Good Parts Chapter 5 Inheritance
Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but ...
- React源码学习——ReactClass
前言 之前一直在使用react做开发,但是对其内部的工作机制却一点儿都不了解,说白了就是一直在套api,毫无成就感.趁最近比较闲,对源码做了一番研究,并通过博客的方式做一些记录. 进入正题 通过编写自 ...
随机推荐
- OGNL表达式的基本语法和用法
首先我们一起来看一下OGNL中的#.%和$符号. 关于OGNL各种用法总结参看:http://blog.163.com/seara520@126/blog/static/720693042010320 ...
- 深入剖析 linux GCC 4.4 的 STL string
转自: 深入剖析 linux GCC 4.4 的 STL string 本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Wri ...
- dp之多重背包2191
水题........ #include<iostream> #include<stdio.h> #include<string.h> using namespace ...
- sql 记录
INSERT INTO B([name],[info]) SELECT [name,'10'] FROM A 级联更新1:update tb1, tb2 set tb1.a=tb2.a,tb1.b=t ...
- Scala中List(Map1,Map2,Map3 ....) 转成一个Map
这个问题研究好久...头大,不记得有fold用法了. fold函数:折叠,提供一个输入参数作为初始值,然后大括号中应用自定义fun函数并返回值. list.fold(Map()){(x,y)=> ...
- ApplicationListener接口中的onApplicationEvent被调用两次解决方式
Spring容器初始化完毕后,调用BeanPostProcessor这个类,这个类实现ApplicationListener接口,重写onApplicationEvent方法, 方法中就是我们自己要在 ...
- sql循环插入测试数据
declare @i int set @i=1while @i<61 begin insert into T_RolePower values(1,@i,1)set @i=@i+1 end
- 解决Maven项目 Missing artifact jdk.tools:jdk.tools:1.7的错误
因学习项目需要,在pom.xml添加hbase-client依赖的时候提示解决Maven工程中报 Missing artifact jdk.tools:jdk.tools:1.7的提示信息,之前遇到这 ...
- 【BZOJ】1611: [Usaco2008 Feb]Meteor Shower流星雨(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1611 一眼题,bfs. #include <cstdio> #include <c ...
- SharePoint Survey WebPart 调查 Web部件
SharePoint Survey WebPart 调查 Web部件 Web部件下载地址 点击此处下载. 安装激活Web部件 过程简单此处省略. 项目描写叙述 调查是SharePoint中协同门户的一 ...