javascript 私有方法的实现

本文详细介绍了在JavaScript中定义类的最佳实践,包括如何实现私有成员、使用匿名函数、创建命名空间以及利用闭包来维护状态。通过实例展示了如何在特定命名空间下创建类,并使用实例方法、静态方法和私有静态成员。此外,文章还讨论了如何通过别名简化代码,以提高可读性和效率。

原文地址:

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.

转载于:https://www.cnblogs.com/oxspirt/p/4487355.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值