原文地址:

http://frugalcoder.us/post/2010/02/11/js-classes.aspx

Classy JavaScript - Best Practices

11. February 2010 13:26

Okay, so you really want to be able to have some of your JavaScript methods to have access to a variable that is private, but maintains state between calls. The first piece of knowledge, is that you can have the contents of a function execute itself at runtime.

1.(function(){ /*Your actions here*/ })();

This is a very common method of defining complex classes and libraries, that can have their own variables or methods that aren't otherwise available to the object model outside this closure. When you utilize "this" within the function's closure it will be default to the global object, which in the Browser DOM is "window".

1.(function(){
2.this.test = "Test Value";
3.})();
4.alert(test); //alerts "Test Value"

Usually when creating libraries in JavaScript it's a good idea to create namespaces for your library. Below I am going to use a classic example for defining a namespace of My.Namespace. There are helper methods out there that will walk a chain from a literal string of "My.Namespace", but I'm showing it in raw script.

1.if (typeof My == 'undefined')
2.My = {};
3.if (typeof My.Namespace == 'undefined')
4.My.Namespace = {};

By combining the above method, and using the Function.prototype.call method on your anonymous function, you can call the function with "this" set to your namespace. I'll be implementing a class called "SomeClass" within "My.Namespace" below. I'll also be showing how to create private static members and methods, allong with public static methods, and instance methods.

01.//begin private closure
02.(function(){
03. 
04.//this is a private static member that is only available in this closure
05.var instances = 0;
06. 
07.//this is a private static method that can be used internally
08.function _incrementInstances() {
09.instances++;
10.}
11. 
12.//Define SomeClass (js uses functions as class constructors, utilized with the "new" keyword)
13.this.SomeClass = function(options) {
14.//if the function is called directly, return an instance of SomeClass
15.if (!(this instanceOf SomeClass))
16.return new SomeClass(options);
17. 
18.//call static method
19._doSomething();
20. 
21.//handle the options initialization here
22.}
23. 
24.//create a public static method for SomeClass
25.this.SomeClass.getInstanceCount = function() {
26.return instances; //returns the private static member value
27.}
28. 
29.//create an instance method for SomeClass
30.this.SomeClass.prototype.doSomething = function() {
31./*Do Something Here*/
32.}
33. 
34.//end private closure then run the closure, localized to My.Namespace
35.}).call(My.Namespace);

The above is an example of best practices for defining a Class within a given namespace. From here, you can instantiate an instance of "My.NameSpace.SomeClass" and utilize the public methods exposed.

01.//instantiate a SomeClass instance
02.var sc = new My.Namespace.SomeClass({/* options go here */});
03. 
04.//call SomeClass as a function, which will return an instance
05.//  defined above via "(!(this instanceOf SomeClass))"
06.var sc = My.Namespace.SomeClass({/* options */});
07. 
08.//view the instance count, which uses a public static method
09.//  to return a private static member.
10.alert(My.Namespace.SomeClass.getInstanceCoun());

From here, you may be thinking to yourself, that's a lot of typing. This is where aliasing can come in handy, in this example inside a closure of course.

01.(function(){
02.//alias My.NameSpace
03.var m = My.NameSpace
04. 
05.//bad form assigning onload internally,
06.//  but that'll be for another post on event binding
07.//  Also, we could use "this.onload" but using window directly is more obvious here.
08.window.onload = function() {
09.//attach an instance of My.NameSpace.SomeClass instance to window.
10.window.sc = new m.SomeClass({}); //no long namespace name here :)
11.}
12. 
13.})();

Hopefully this post will be helpful in utilizing some privacy with your classes, and using namespaces to prevent naming collisions with other classes, and libraries.

javascript 私有方法的实现的更多相关文章

  1. JavaScript 类私有方法的实现

    一:将私有方法移出模块,因为模块内部的所有方法都是对外可见的. class Widget { foo (baz) { bar.call(this, baz); } // ... } function ...

  2. JavaScript中子类调用父类方法的实现

    一.前言 最近在项目中,前端框架使用JavaScript面向对象编程,遇到了诸多问题,其中最典型的问题就是子类调用父类(super class)同名方法,也就是如C#中子类中调用父类函数base.** ...

  3. JavaScript内置一些方法的实现原理--new关键字,call/apply/bind方法--实现

    先学习下new操作符吧 new关键字调用函数的心路历程: 1.创建一个新对象 2.将函数的作用域赋给新对象(this就指向这个对象) 3.执行函数中的代码 4.返回这个对象 根据这个的思路,来实现一个 ...

  4. Atitit paip.对象方法的实现原理与本质.txt

    Atitit paip.对象方法的实现原理与本质.txt 对象方法是如何实现的1 数组,对象,字典1 对象方法是如何实现的 这显然是一个对象方法调用.但对象方法是如何实现的呢?在静态语言中,因为有编译 ...

  5. OC:属性的内部实现原理、dealloc内释放实例变量、便利构造器方法的实现原理、collection的内存管理

    代码: // // main.m #import <Foundation/Foundation.h> #import "Person.h" #import " ...

  6. 基于原生JS的jsonp方法的实现

    基于原生JS的jsonp方法的实现 jsonp,相信大家并不陌生,是在js异步请求中解决跨域的方法之一,原理很简单,有不清楚的同学可以google下,这里就补详细解释了.在Jquery库中,jQuer ...

  7. 关于Java中hashCode方法的实现源码

    首先来看一下String中hashCode方法的实现源码. public int hashCode() { int h = hash; if (h == 0 && value.leng ...

  8. js中的bind方法的实现方法

    js中目前我遇见的改变作用域的5中方法:call, apply, eval, with, bind. var obj = { color: 'green' } function demo () { c ...

  9. Salesforce LWC学习(十五) Async 以及 Picklist 公用方法的实现

    本篇参考:salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type) https://developer.salesfo ...

随机推荐

  1. 构建一个真实的应用电子商务SportsStore9

    使用MVC4,Ninject,EF,Moq,构建一个真实的应用电子商务SportsStore(九) 实在不好意思,好久没有更新了,我不想找些客观原因来解释,只想请大家见谅!现在我们继续我们的项目,客户 ...

  2. Arduino 不同Arduino衍生板子的问题

    arduino IDE装上的时候,要记得在windows平台安装驱动. 如果不安装驱动的话,烧写程序的时候也许会遇到下面的现象. 原因有如下几种: 1,arduino控制板或者COM口没有选对,这种问 ...

  3. 解析JSON、扩展Fiddler

    解析JSON.扩展Fiddler 按文章结构,这部分应该给出WCFRest项目示例,我想WinForm示例足够详尽了,况且WCFRest还不需要使用插件AppDomain那一套,于是把最近写的Fidd ...

  4. 设置session超时

    在web应用中,设置session超时有三种方法: 1.在web.xml文件中配置:单位是分钟,范围是针对本项目所有用户的session <session-config> <sess ...

  5. Java7新特性

    ① 新增了switch对字符串的支持,也就是说可以在switch之后直接使用字符串来进行判断,语法基本与Java7之前支持的语法一样. ② 对数值字面量的增强支持,首先是可以在源代码中直接使用二进制数 ...

  6. ASP.NET Zero--5.配置权限

    修改角色的时候,会有一份权限列表,可以给这个角色分配哪些权限,那如何添加一个新权限呢?   这里以添加一个“测试”的权限为例 1.打开AppPermissions.cs [..\MyCompanyNa ...

  7. vsftpd.conf 详解与实例配置

    #################匿名权限控制############### anonymous_enable=YES #是否启用匿名用户      no_anon_password=YES #匿名用 ...

  8. linux服务器开发二(系统编程)--线程相关

    线程概念 什么是线程 LWP:Light Weight Process,轻量级的进程,本质仍是进程(在Linux环境下). 进程:独立地址空间,拥有PCB. 线程:也有PCB,但没有独立的地址空间(共 ...

  9. .net在网页中生成二维码和条形码

    二维码: 1.下载ThoughtWorks.QRCode.dll文件 2.创建Web项目,添加引用刚才下载的文件 3.在项目中添加aspx窗体,编写代码如下 <%@ Page Language= ...

  10. CodeVS 3415-最小和

    原题 题目描述 Description      小浣熊松松来到文具店,选择了K支自己喜欢的水彩笔,并抄下了它们的价格.可是到结算时,他发现自己抄价格时抄得太密集,以至于所有价格连成了一个数字串(你可 ...