`
zjlovezj
  • 浏览: 28548 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

public privileged private -- method varible

阅读更多

<script>
 //class function for defining instances of a closure demo
 function ClosureDemo2() {
  var _a = 0, _b = 0;     //private properties prefixed with an underscore to denote that they
        //cannot be accessed outside of the instance
  
  this.c = 0;  //public property
  
  //private function declared as a variable
  var _doSomething = function() {
   alert('doing something...');
  };
 
  //private function declared as a function shorthand for var _doAnotherSomething = function() {}
  function _doAnotherSomething() {
   alert('doing another something...');
  }
 
  //privileged and public function privileged, because it can access the private vars
  this.Increment = function() {
   _a++; //this works here
   _b++; //this works here
   this.c++; //this, of course, works here
  };
  
  //privileged and public function privileged, because it can access the private vars
  this.Print = function() {
   _doAnotherSomething(); //this works here
  
   alert("a = " + _a.toString() + "\n" +"b = " + _b.toString() + "\n" +"c = " + this.c.toString() + "\n");
  };
  
  alert('initializing an instance of this class...');
  _doSomething(); //this works here
  
  //*NOTE: The following will not work  //这个与Java不同
  //this._a, this._b, this._doSomething(), this._doAnotherSomething()         
 }
 
 
 
 
 var myDemo1 = new ClosureDemo2();
 
 ClosureDemo2.prototype.IncrementAgain = function() {         //public method can access to public var,
                 //但是不能访问private var, 这个与Java不同
  //_a++; //this fails here
  //_b++; //this fails here
  this.c++; //this works here
 };
 
 var myDemo2 = new ClosureDemo2();
 
 myDemo1.Increment(); //this will work here
 myDemo2.Increment(); //this will work here
 
 //these 2 lines will fail here
 //myDemo1._a = 2;
 //myDemo1._b = 2;
 
 myDemo1.c = 2; //this will work here
 myDemo2.c = 2; //this will work here
 
 //these 2 lines will also fail here
 //myDemo1._doSomething();
 //myDemo1._doAnotherSomething();
 
 //because the prototype of the class was changed after myClass1 was created, this fails。
 //myDemo1.IncrementAgain();         //这个就太奇怪了,public method要使用前添加
 
 //but, this works
 myDemo2.IncrementAgain();
 
 myDemo1.Print(); //this will work here
 myDemo2.Print(); //this will work here
</script>
 

 

 

In the code below there is a scoping error:

function ClosureDemo()
{
  var a = b = 0;                 /*

The first error is in the declaration of the a and b variable.  In the above code, I only declared the variable a in the scope of the function.  Variable b is not declared, and was merely initialized to 0.  Instead, since variable b is not being declared, the variable will be created (if necessary) in the global scope and initialized to 0.  Now, b, instead of being a variable local to each instance of the closure, acts as a global variable. */

 
  this.Test = function()
  {
    a++;
    b++;
  }
}

分享到:
评论

相关推荐

    riscv-privileged-spec-v1.7中文版

    "RISC-V Privileged Spec v1.7中文版"是针对RISC-V特权级的规范文档,由EETOP社区的用户“要你命3000”翻译,为中文使用者提供了方便的学习资料。 在RISC-V体系结构中,“特权模式”指的是CPU运行的不同级别,这些...

    riscv-privileged-20211203.pdf

    它分为用户级(User)和特权级(Privileged)指令集,后者涉及到操作系统和硬件管理的核心功能。 在20211203版中,我们看到以下几个关键模块的更新和状态: 1. **机器ISA(Machine ISA)**:版本1.12已批准。这是...

    riscv-privileged.pdf

    RISC-V Privileged Architecture RISC-V 是一个开源的 RISC 指令集处理器,由伯利克利学校师生发明,已经应用于多个领域,例如勘智的 K210,可以应用于人脸识别或者其他控制领域。下面是 RISC-V Privileged ...

    riscv-privileged-20190608-1.pdf

    标题中的“riscv-privileged-***-1.pdf”指的是文档的名称和版本日期,表示这是RISC-V特权架构规范的版本,发布时间为2019年6月8日。文档中提到了“CSR寄存器”,即控制状态寄存器(Control and Status Registers)...

    riscv-privileged-20190608.pdf

    RISC-V是一种开源指令集架构(ISA),起初由加利福尼亚大学伯克利分校的研究团队设计,并由RISC-V基金会进行管理。RISC-V指令集支持一系列计算机处理器设计,可应用于各种计算设备,从微控制器到超级计算机。...

    riscv-privileged-draft-20191214-d98903e.pdf

    - **"riscv-privileged-draft-20191214-d98903e.pdf"**:此文档为RISC-V指令集手册中关于特权架构的部分,版本号为1.12-draft,发布日期为2019年12月14日。 #### 描述解析 - **"RISC-V 指令集手册"**:这是一份详细...

    JavaScript的public、private和privileged模式

    在探讨JavaScript编程中,公共(public)、私有(private)和特权(privileged)模式是面向对象编程中用于管理对象内部成员的重要概念。JavaScript虽然是基于原型的语言,但它同样可以实现类似面向对象语言中的封装...

    docker-qemu-reactos:用于ReactOS操作系统的Docker映像

    --privileged --env QEMU_KVM=true \ docker.io/hectormolinero/qemu-reactos:latest 该实例将通过Web浏览器从以下位置提供: 环境变量 QEMU_CPU VM允许使用的核心数(默认为2 )。 QEMU_RAM VM允许使用的内存...

    docker-cjdns:docker 中的 cjdns

    docker-cjdns 安装很简单。... 这可以使用--privileged --net=host选项--privileged --net=host 。 docker pull mildred/cjdnsdocker run --privileged --net=host mildred/cjdns --volume /etc/cjdns:/etc/cjdns

    MyLOL特权「MyLOL Privileged」-crx插件

    免费获得MyLOL特权! 免费获得MyLOL特权! 支持语言:English (UK)

    es以及kibana的镜像压缩包

    -v es-plugins:/usr/share/elasticsearch/plugins --privileged --network es-net -p 9200:9200 -p 9300:9300 elasticsearch:7.12.1 docker run -d \ --name kibana \ -e ELASTICSEARCH_HOSTS=http://es:9200 \...

    mocknet-rust

    sudo docker run --net=host --privileged --entrypoint /workspace/indradb -v /tmp:/tmp -v /home/djp/indradb/target/debug:/workspace --name indradb -it -d ubuntu:18.04 用于启动server_main容器的命令: ...

    linux crontab使用介绍

    crontab是linux下最最常用的定时任务,本文档详细介绍了使用方法,供参考

    镜像名称是:rancher/rancher:v2.4.17

    docker run -d --restart=unless-stopped --privileged \ -e CATTLE_SYSTEM_CATALOG=bundled \ -p 8888:80 -p 8443:443 \ -v /var/lib/rancher:/var/lib/rancher \ -v /opt/rancher/ssl/FULL_CHAIN.pem:/etc/...

    cadvisor.tar.gz

    ro --volume=/var/run:/var/run:ro --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --volume=/dev/disk/:/dev/disk:ro --publish=8080:8080 --detach=true --name=cadvisor --privileged --...

    实验1:Hadoop安装部署

    docker run --name master --privileged --hostname master --ip 172.18.0.2 --add-host=slave1:172.18.0.3 --add-host=slave2:172.18.0.4 --add-host=slave3:172.18.0.5 -itd -v /cgsrc:/cgsrc:ro -v /headless/...

Global site tag (gtag.js) - Google Analytics