js in depth: closure function & curly function

闭包, 科里化

new

js 构造函数 实例化, 不需要 new


var num = new Array();
for(var i=0; i<4; i++){
num[i] = f1(i);
}
function f1(n){
var i=0;
function f2(){
i++;
console.log(i, n);
}
return f2;
}
num[2]();
num[1]();
num[0]();
num[3](); // 1 2
// 1 1
// 1 0
// 1 3

https://www.cnblogs.com/hejun26/p/10910590.html

new

js 构造函数 实例化, 不需要 new === 调用常规方法

js 构造函数 实例化, 需要 new === 类作用,实例化新的实例


var num = new Array();
for(var i=0; i<4; i++){
num[i] = f1(i);
console.log(num[i] instanceof f1);
}
function f1(n){
var i=0;
function f2(){
i++;
console.log(i, n);
}
return f2;
}
num[2]();
num[1]();
num[0]();
num[3]();

如果不是创建类多个实例的话,为什么每个 f1 分别维护一套 i 的变量呀?不是因该共享的吗?

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


js in depth: closure function & curly function的更多相关文章

  1. js in depth: arrow function & prototype & this & constructor

    js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...

  2. js in depth: Object & Function & prototype & __proto__ & constructor & classes inherit

    js in depth: Object & Function & prototype & proto & constructor & classes inher ...

  3. js 深入原理讲解系列-currying function

    js 深入原理讲解系列-currying function 能看懂这一题你就掌握了 js 科里函数的核心原理 不要专业的术语,说人话,讲明白! Q: 实现 sum 函数使得以下表达式的值正确 cons ...

  4. js 里面的 function 与 Function

    function 是 js 的标识符 Function 是 js 里面的一个 构造函数 1.new function 与 new Function 的区别 new 运算符在 js 里面是 创建一个自定 ...

  5. JS里面function和Function的区别

    js里Function 与 function的不一样的,不仅仅是大小写的问题. 简单点说:大写的Function是一个类 ,而小写的function是一个对象. Function是一个构造器,func ...

  6. 解决掉你心中 js function与Function的关系的疑问

    前言 在网上有很多关于js function 与 Function直接关系的文章. 但是我感觉过于抽象化了,那么如何是具体化的解释? 正文部分为个人理解部分,如有不对望指出. 正文 <scrip ...

  7. JS原型的问题Object和Function到底是什么关系

    var F = function(){}; Objcert.prototype.a = function(){}; Function.prototype.b = function(){}; F 既能访 ...

  8. js立即执行函数: (function ( ){...})( ) 与 (function ( ){...}( ))

    ( function(){…} )() ( function (){…} () ) 是两种javascript立即执行函数的常见写法,最初我以为是一个括号包裹匿名函数,再在后面加个括号调用函数,最后达 ...

  9. 深入理解js中的立即执行函数(function(){…})()

    javascript和其他编程语言相比比较随意,所以javascript代码中充满各种奇葩的写法,有时雾里看花,当然,能理解各型各色的写法也是对javascript语言特性更进一步的深入理解. ( f ...

随机推荐

  1. OSS与文件系统的对比 文件存储 块存储 对象存储

    基本概念介绍_开发指南_对象存储 OSS-阿里云  https://help.aliyun.com/document_detail/31827.html 强一致性 Object 操作在 OSS 上具有 ...

  2. 浅谈正向代理、反向代理和CDN的区别

    一.正向代理 1.正向代理位于客户端和源服务器之间的服务器(代理服务器): 2.隐藏客户端:由代理服务器代替客户端去访问目标服务器,用户需要设置代理服务器的IP和端口: 3.每一次请求是到代理服务器, ...

  3. 周期性清除Spark Streaming流状态的方法

    在Spark Streaming程序中,若需要使用有状态的流来统计一些累积性的指标,比如各个商品的PV.简单的代码描述如下,使用mapWithState()算子: val productPvStrea ...

  4. nginx详解、反向代理、负载均衡和LNMP架构

    资源池 nginx官方网站:http://nginx.org/ nginx官方文档:http://nginx.org/en/docs/ 本章资源: 点击这里 资源提取码:u2jv 1.nginx简介 ...

  5. vue-cli-----vue实例中template: '<App/>',这样写是什么意思

    我刚开始学,看这个东西看了好久,官网文档的描述我不太理解,今天终于算明白了 官网的描述: 模板将会替换挂载的元素.挂载元素的内容都将被忽略 也就是说:template: '<App/>' ...

  6. Centos8上搭建EMQ MQTT

    layout: post title: Centos8上搭建EMQ MQTT subtitle: 在阿里云Centos8搭建EMQ并配置接入 date: 2020-3-11 author: Dapen ...

  7. The Preliminary Contest for ICPC Asia Nanjing 2019 A The beautiful values of the palace(树状数组+思维)

    Here is a square matrix of n * nn∗n, each lattice has its value (nn must be odd), and the center val ...

  8. 1. Machine Learning - Introduction

    Speaker: Andrew Ng   1. Introduction 1.A comptuter program is said to learn from experience E with r ...

  9. Codeforces Round #673 (Div. 2) D. Make Them Equal(数论/构造)

    题目链接:https://codeforces.com/contest/1417/problem/D 题意 给出一个大小为 $n$ 的正整数数组 $a$ ,每次操作如下: 选择 $i,j$ 和 $x$ ...

  10. HDU6434 Count【欧拉函数 线性筛】

    HDU6434 I. Count T次询问,每次询问\(\sum_{i=1}^{n}\sum_{j=1}^{n-1}[gcd(i-j,i+j)=1]\) \(T\le 1e5, n \le 2e7\) ...