块级作用域和私有变量

手把手教你用vivado2018.3为复旦微JFMQL15T打IP补丁(含环境变量配置技巧) 本文详细介绍了在Vivado 2018.3环境中为复旦微JFMQL15T开发板打IP补丁的全流程,包括环境变量配置、IP缓存管理等关键步骤。通过实战验证的解决方案,帮助工程师快速搭建开发环境,解决国产芯片开发中的常见问题,提升开发效率。 阅读详情

块级作用域

(function(){
    //这里是块级作用域
})()

例如:

(function(){
    for(var i=0;i<5;i++){
        alert(i);//0,1,2,3,4
    }
})();
alert(i);//error

上例中,定义了一个块级作用域,变量i在块级作用域中可见的,但是在块级作用域外部则无法访问。

这种技术经常在全局作用域中被用在函数外部,从而限制向全局作用域中添加过多的变量和函数。

私有变量

任何在函数中定义的变量,都可以认为是私有变量。因为不能在函数的外部访问这些变量。私有变量包括函数的参数、局部变量和在函数内部定义的其他函数。

我们把有权访问私有变量和私有函数的公有方法称为特权方法:

function MyObject(){
//私有变量和私有函数
    var privateVariable = 10;
    function privateFunction(){
        return false;
    }
    //特权方法,特权方法作为闭包有权访问在构造函数中定义的所有变量和函数
    this.publicMethod = function (){
        privateVariable++;
        return privateFunction();
    };
}
var obj = new MyObject();
obj.publicMethod();//

原型模式定义共有方法:

(function(){
    //私有变量和私有函数
    var privateVariable = 10;
    function privateFunction(){
        return false;
    }
    //构造函数
    MyObject = function(){
    };
    //公有/特权方法
    MyObject.prototype.publicMethod = function(){
        privateVariable++;
        return privateFunction();
    };
})();

模块模式:

var singleton = function(){
//私有变量和私有函数
    var privateVariable = 10;
    function privateFunction(){
        return false;
    }
    //特权/公有方法和属性
    return {
        publicProperty: true,
        publicMethod : function(){
            privateVariable++;
            return privateFunction();
        }
    };
}();
【数字IC验证进阶】正则表达式(Regular Expression)快速入门指南 在芯片开发过程中,正则表达式的使用非常常见。初次上手晦涩难懂,多用几次爱不释手! 阅读详情

相关推荐

MMORPG网络游戏开发之任务系统的架构与设计

在网路游戏中做任务已经成为游戏很重要的一个核心功能和玩法,如何做好一个灵活可扩展的任务系统的架构与设计,今天来给大家分享一些我们的设计经验。接下来我把整个的任务系统分成以下6个模块:对于单机游戏而言,这6个模块都放在客户端直接处理,对于网路游戏而言,模块1~5实现在服务端,模块6实现在客户端。对啦!这里有个游戏开发交流小组里面聚集了一帮热爱学习unity的零基础小白,也有一些正在从事unity开发的技术大佬,欢迎你来交流学习。...

voidinit的专栏 3049

变量的作用域

【代码】变量的作用域。

shan_03的博客 294

内网开发必备:5分钟搞定WSL2离线安装(含Ubuntu迁移避坑指南)

本文详细介绍了在内网环境下快速完成WSL2离线安装的完整流程,包括系统兼容性检查、Linux内核更新包安装以及Ubuntu镜像的离线部署。特别针对外网Ubuntu环境迁移至内网时的用户权限、系统路径等常见问题提供了实用解决方案,帮助开发者高效搭建Linux开发环境。

weixin_29255731的博客 408

块级作用域变量

1. let var test = 0 ; {      var test = 1 ; } test // 1 如果改用let var test = 0 ; {      let test = 1 ;  //真正的局部变量 } test // 0 2. const     const test = 0 ;     test  = 1 ;

启 424

通义万相Wan2.1模型Lora训练以及推理

使用DiffSynth-Studio进行通义万象Wan2.1的Lora训练和推理

qq_64858624的博客 4608

[南大ICS-PA2] 字符串处理函数和printf实现

[南大ICS-PA2] 程序、运行时环境与AM 字符串处理函数和printf实现

qq_42474104的博客 2988

nodejs4:node怎么访问一个模块作用域的变量(require的两个作用)

node没有全局作用域,只有模块作用域: 怎么访问另一个模块作用域的成员变量 // require方法有两个作用 // 1,加载文件模块并执行里面的代码 // 2,拿到被加载文件模块到处的接口对象 // 在每一文件模块中都提供了一个对象 export // export 默认是一个空对象 // 所以可以将需要被外部加载的成员 挂载到这个exports对象中 文件a.js var bExport...

weixin_43342105的博客 633

SAP 行业扫盲系列之一:SAP 从业者的分类

本文简单介绍了 SAP 从业者的分类,限于笔者狭隘的视角,可能存在遗漏或者不恰当的地方,希望大家在评论区指出。 那些在知乎咨询上向我提问关于进入 SAP 行业建议的朋友们,也请大家提问时注明自己想具体从事 SAP 行业哪一类的工作,这样才能得到更让你们满意的回复。感谢阅读。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1590

vue学习笔记---块级作用域

笔记: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <button>按钮1</button> <butto...

多哥仁慈的骆驼 450

人工智能机器学习基础篇】——深入详解强化学习 基础知识,理解马尔可夫决策过程(MDP)、策略、价值函数等关键概念

人工智能机器学习基础篇】——深入详解强化学习 基础知识,理解马尔可夫决策过程(MDP)、策略、价值函数等关键概念

编程技术探索者,分享C/C++、C#、Java、数据库等开发经验,聚焦实战技巧与AI兴趣,助力编程爱好者成长。 1932

【PINN物理信息网络】基于PINN物理信息网络求解瞬态薛定谔方程(python)

【PINN物理信息网络】基于PINN物理信息网络求解瞬态薛定谔方程(python)

k8291121的博客 2724

JavaScript中的块级作用域和私有变量

模仿块级作用域: JavaScript没有块级作用域的概念。这意味着在块语句中定义的变量,实际上是在包含函数中而非语句中创建的。 看一个例子:  function outputNumbers(count){          for(vari=0;i            alert(i); } alert(i); //计数 } 这个函数中定义了一个for循环,变量i的初始值被设

xin_xun_的博客 741

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

JavaScript闭包-块级作用域和私有变量

块级作用域 块级作用域是指在由花括号包裹的代码块中的作用域。在JavaScript中是没有块作用域的。为了理解这个概念,来看下面的例子: for(var i = 0;i   ...... } alert(i); //结果会输出10     上面的代码中,我们在for循环中定义了变量i,在C++和Ja

飞鹰的专栏 1486

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

js闭包实现块级作用域和私有变量的访问

1、块级作用域 首先看下面的例子 function  test1(){ for(var i=0;i } alert(i); } test1();//10 上面的函数说明了变量i的作用域是整个函数,而不是for循环内,那么怎么才能 function  test2(){ for(var i=0;i }         var i; alert(i); } test

zhanglongfei1016的专栏 1111

JavaScript 模仿块级作用域和私有变量

这是再次看《JS高级程序设计 》做的笔记,加深印象。也供大家共同学习 js中没有块级作用域的概念。看下面例子: function outputName(count){ for(var =0;i<count;i++){ alert(i); } alert(i);//count } 在java,c++中,变量i只会在for循环中有定义,循环一旦结束,变量就会被销...

weixin_30618985的博客 65

js html 5 产量作用域,JavaScript闭包-块级作用域和私有变量

块级作用域块级作用域是指在由花括号包裹的代码块中的作用域。在JavaScript中是没有块作用域的。为了理解这个概念,来看下面的例子:for(var i = 0;i < 10; i++){......}alert(i); //结果会输出10上面的代码中,我们在for循环中定义了变量i,在C++和Java等编程语言中,循环执行结束之后,for循环中的i变量会立刻被垃圾回收。但是在JavaScr...

weixin_42299346的博客 159

第七章:函数表达式(模仿块级作用域和私有变量)

函数表达式模仿块级作用域 JS和JAVA一个区别就在于JS没有块级作用域。看以下例子: function outputNumbers(count){ for (var i=0; i < count; i++){ alert(i); } //var i;//多了这句对结果毫无影响 alert(i); /

miaoch的博客 325

JavaScript中的块级作用域、私有变量与模块模式

1.块级作用域(私有作用域),经常在全局作用域中被用在函数外部,从而限制向全局作用域中添加过多的变量和函数。 (function(count){ for(var i=0;i<count;i++){ console.log(i);//=>0、1、2、3、4 } console.log(i);//=>5 })(5);(function(){ var now=new Date(); if

bboyjoe的博客 637

JS之 函数模仿块级作用域,私有变量,模块模式

模仿块级作用域 Javascript没有块级作用域的概念,意味着在块级语句中定义的变量,实际上是在包含函数中而非语句中创建的,看下面例子: function a() { for (var i = 0; i < 2; i++) { alert(i);// 0,1 } alert(...

weixin_34391854的博客 78

《javascript高级程序设计》——模仿块级作用域

javascript没有块级作用域的概念。所以形如一下代码  function outputNumbers(count) { for(var i=0;i<count;i++){ var sum = 0; sum += i; } alert(i); } outputNumbers(4); 这个函数执行后会弹窗显示4。在Java,C++等语言中,变量i只会在for

start_heart的专栏 542

闭包-块级作用域

块级作用域 块级作用域是指在由花括号包裹的代码块中的作用域。在JavaScript中是没有块作用域的。为了理解这个概念,来看下面的例子: for(var i = 0;i   ...... } alert(i); //结果会输出10     上面的代码中,我们在for循环中定义了变量i,在C++和Ja

yi_xiuge的博客 602

Js如何构造块级作用域,构造私有变量

众所周知,Js是没有块级作用域的,但可以使用匿名函数来模仿块级作用域。 1. for循环中定义的变量,从他有定义开始就可以在函数内随处访问他。 可以看出,for循环内定义的变量并没有块级作用域。注意:必须调用函数,函数内的变量才可以声明。 2. 用匿名函数来模仿块级作用域(私有作用域) 这里在块级作用域(私有作用域)可以访问count的原因是,匿名函数是外部函数的内包。 优点:这...

wurourouzhang的博客 234

JS学习笔记——私有变量

0.本章提要 使用立即执行函数实现块级作用域 使用构造函数实现实例私有变量:私有变量函数和公有特权方法每个实例都有独立的一份 使用原型模式+块级作用域实现静态私有变量:私有变量函数和公有特权方法每个实例都共享一份 1.块级作用域 javascript不像其他语言有块级作用域,比如C、java的块级作用域。 //C、java块级作用域 for(int i = 0; i < N; i++){.....

weixin_30342209的博客 120

模仿块级作用域

在Java、C++等语言中,变量i只会在for循环的语句块中有定义,循环一旦结束,变量i就会被销毁。可是在JavaScrip中,变量i是定义在ouputNumbers()的活动对象中的,因此从它有定义开始,就可以在函数内部随处访问它。而在私有作用域中能够访问变量count,是因为这个匿名函数是一个闭包,它能够访问包含作用域中的所有变量。其中的变量now现在是匿名函数中的局部变量,而我们不必在全局作用域中创建它。这意味着在块语句中定义的变量,实际上是在包含函数中而非语句中创建的,来看下面的例子。

zf888999666的博客 107

【学习笔记六】 - js中 创建对象的模式与继承 及 js中实现块级作用域和函数私有变量 《js高程》6-7笔记

学习笔记、《js高程》、创建对象的不同模式、js继承、js函数私有变量、js中实现块级作用域(声明提前)

Jiuto_crx的博客 422
上一篇: 闭包
下一篇: BOM(一)
程序员青戈
程序员青戈 领域专家: 后端开发技术领域 领域专家: 后端开发技术领域
博客等级 码龄10年 3万+粉丝 250原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员青戈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值