- 浏览: 311395 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (243)
- Core Java (13)
- Java (12)
- Android (2)
- Lucene (3)
- J2EE (3)
- Hibernate (2)
- Jsp & Servlet (3)
- Struts2 (3)
- Spring (5)
- JSF (6)
- RichFaces (2)
- HTML & JS & DOM & CSS (87)
- browser (1)
- Ajax & A4J (2)
- Workflow (1)
- Maven (3)
- Linux (2)
- VM & VBOX (1)
- Tomcat (1)
- Cache (3)
- Others (36)
- design (1)
- PHP (1)
- Try.js (1)
- HTML5 && CSS3 && ECMAScript5 (26)
- 疯言疯语 (5)
- mongodb (2)
- Hardware Common Sence (1)
- RESTful (3)
- Nginx (2)
- web安全 (8)
- Page Design (1)
- web performance (1)
- nodejs (4)
- python (1)
最新评论
-
New_Mao_Er:
求最新的版本破解啊!!!
Omondo eclipseUML插件破解 -
konglx:
讲得仔细,谢了
全面分析 Spring 的编程式事务管理及声明式事务管理 -
cilendeng:
对所有post有效只能使用过滤器
说说Tomcat的Server.xml的URIEncoding=‘UTF-8’配置 -
jiulingchen:
mark了,灰常感谢!
JAVA中最方便的Unicode转换方法 -
anlaetion:
这算法可以有
js 字符串搜索算法
Private Members in JavaScript
Douglas
Crockford
www.crockford.com
JavaScript is the world's most misunderstood programming language . Some believe that it lacks the property of information hiding because objects cannot have private instance variables and methods. But this is a misunderstanding. JavaScript objects can have private members. Here's how.
Objects
JavaScript is fundamentally about objects . Arrays are objects. Functions are objects. Objects are objects. So what are objects? Objects are collections of name-value pairs. The names are strings, and the values are strings, numbers, booleans, and objects (including arrays and functions). Objects are usually implemented as hashtables so values can be retrieved quickly.
If a value is a function, we can consider it a method . When a method of an object is invoked, the this variable is set to the object. The method can then access the instance variables through the this variable.
Objects can be produced by constructors , which are functions which initialize objects. Constructors provide the features that classes provide in other languages, including static variables and methods.
Public
The members of an object are all public members. Any function can access, modify, or delete those members, or add new members. There are two main ways of putting members in a new object:
In the constructor
This technique is usually used to initialize public instance variables. The constructor's this variable is used to add members to the object.
function Container(param) { this.member = param; }
So, if we construct a new object
var myContainer = new Container('abc');
then myContainer.member contains 'abc' .
In the prototype
This technique is usually used to add public methods. When a member is sought and it isn't found in the object itself, then it is taken from the object's constructor's prototype member. The prototype mechanism is used for inheritance. It also conserves memory. To add a method to all objects made by a constructor, add a function to the constructor's prototype :
Container.prototype.stamp = function (string) { return this.member + string; }
So, we can invoke the method
myContainer.stamp('def')
which produces 'abcdef' .
Private
Private members are made by the constructor. Ordinary var s and parameters of the constructor becomes the private members.
function Container(param) { this.member = param; var secret = 3; var that = this; }
This constructor makes three private instance variables: param , secret , and that . They are attached to the object, but they are not accessible to the outside, nor are they accessible to the object's own public methods. They are accessible to private methods. Private methods are inner functions of the constructor.
function Container(param) { function dec() { if (secret > 0) { secret -= 1; return true; } else { return false; } } this.member = param; var secret = 3; var that = this; }
The private method dec examines the secret instance variable. If it is greater than zero, it decrements secret and returns true . Otherwise it returns false . It can be used to make this object limited to three uses.
By convention, we make a private that parameter. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions.
Private methods cannot be called by public methods. To make private methods useful, we need to introduce a privileged method.
Privileged
A privileged method is able to access the private variables and methods, and is itself accessible to the public methods and the outside. It is possible to delete or replace a privileged method, but it is not possible to alter it, or to force it to give up its secrets.
Privileged methods are assigned with this within the constructor.
function Container(param) { function dec() { if (secret > 0) { secret -= 1; return true; } else { return false; } } this.member = param; var secret = 3; var that = this; this.service = function () { if (dec()) { return that.member; } else { return null; } }; }
service is a privileged method. Calling myContainer.service() will return 'abc' the first three times it is called. After that, it will return null . service calls the private dec method which accesses the private secret variable. service is available to other objects and methods, but it does not allow direct access to the private members.
Closures
This pattern of public, private, and privileged members is possible because JavaScript has closures . What this means is that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned. This is an extremely powerful property of the language. There is no book currently available on JavaScript programming that shows how to exploit it. Most don't even mention it.
Private and privileged members can only be made when an object is constructed. Public members can be added at any time.
Patterns
Public
function Constructor ( ...) { this. membername = value ; } Constructor .prototype. membername = value ;
Private
function Constructor ( ...) { var that = this; var membername = value ; function membername ( ...) { ...} } Note: The function statement function membername ( ...) { ...} is shorthand for var membername = function membername ( ...) { ...};
Privileged
function Constructor ( ...) { this. membername = function ( ...) { ...}; }
Copyright 2001 Douglas Crockford. All Rights Reserved Wrrrldwide.
[转自 :http://www.crockford.com/javascript/private.html ]
发表评论
-
循环数组的逻辑怎么写
2015-03-23 10:24 643应用场景这样的: var imgUrls = [ ... -
发布`代码生成器`
2014-11-17 00:45 580闲话不说,直接上地址 npm: https://www. ... -
MutationObserver
2014-10-27 15:29 1090MutationObserver MutationObse ... -
a simple mvvm library - bird
2014-10-09 18:26 726see here:https://github.com/i ... -
遍历dom tree是一件相当耗时的事情
2014-09-23 01:15 752遍历dom tree是一件相当耗时的事情,尤其是在遍历的同时 ... -
今天再讲下js里的继承
2014-09-18 00:27 693js的继承说简单也很简单,请看: function ... -
Text 类型
2014-09-05 18:52 841文本节点由Text类型表 ... -
JavaScript插入动态脚本
2014-09-05 18:47 635动态脚本指的是在页面加载时不存在,但将来的某一时刻通过修改该 ... -
innerHTML插入<style>元素
2014-09-05 18:37 1157通过innerHTML写入<style>元素没 ... -
CSS实现相对浏览器窗口定位彻底研究
2014-09-05 18:33 3718Web Developer / Designer 经常需要将 ... -
JavaScript插入动态样式
2014-09-05 18:07 602能够把CSS样式包含到HTML页面中的元素有两个。其中,& ... -
再理解jQuery;delete原型属性
2014-05-13 22:05 1855以前用jQuery的时候曾粗略看了它的源码,但却不求甚解。 ... -
javascript &&和||
2012-07-23 00:38 700一直以为 && 和 || ... -
undefined 和 void 0 的区别
2012-07-20 11:15 701在读backbone源码时看见这么一段代码: if ( ... -
Fiddler - 前端开发值得拥有
2012-07-16 14:41 825最近换了新工作,搬了新家,换了新室友,一切都在重新开始。 ... -
说说我的web前端之路,分享些前端的好书
2012-07-16 14:38 791WEB前端研发工程师,在国内算是一个朝阳职业,这个领域没 ... -
JavaScript实现 页面滚动图片加载
2012-07-16 14:29 685又到了这个月的博客时间了,原计划是打算在这个月做一个的功 ... -
JavaScript 操作 Cookie
2012-07-16 11:18 682从事web开发也有些日 ... -
Javascript定义类(class)的三种方法
2012-07-12 12:35 583程序员们做了很多探索,研究如何用Javascript模拟”类” ... -
服务端解决跨源共享
2012-06-21 10:18 4513跨源资源共享是web开发领域中一个非常有趣的话题,互联网 ...
相关推荐
压缩包内文档名如下: Core JavaScript 1.5 Guide Javascript面向对象 ...Private Members in JavaScript prototype手册 深入理解JavaScript闭包 悟透javascript(很好) 详解Javascript 中的this指针
在探讨JavaScript编程中,公共(public)、私有(private)和特权(privileged)模式是面向对象编程中用于管理对象内部成员的重要概念。JavaScript虽然是基于原型的语言,但它同样可以实现类似面向对象语言中的封装...
1. 为何使用私有成员(Private Members) 私有成员的主要目的是限制对对象内部状态的访问。这在以下场景中尤为重要: - **数据安全性**:防止外部代码意外或恶意地改变对象的状态。 - **封装**:隐藏实现细节,...
// Everything returned in the object literal is public, but can access the members in the closure created above. return { // Public method. stringToArray: function(str, delimiter, stripWS) { if ...
例如,它无法实现私有成员(private members),因为所有的属性和方法最终都会暴露给子类。此外,虽然可以模拟静态方法和属性,但它们并不是真正意义上的静态,因为它们实际上是绑定到构造函数而不是类本身。 ES6...
安装 npm install docme用法 docme <readme> <docme> -- <jsdoc> Generates jsdocs for non-private members of the project in the current folder. It then updates the given README with the githubified ...
String sql = "INSERT INTO members(name, phone, balance) VALUES(?, ?, ?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, this.name); pstmt.setString(2, this.phone); pstmt.setDouble(3, ...
入门创建有权访问聊天API的服务帐户添加ChatService与库ID项目1XDYAGcUoblJ073Kjveni2WOZHpKLYE8qlYMkeHViavLXvsKnBIl1DR2A 将privateKey和issuerEmail发送到ChatService.init方法返回的对象具有Spaces , Members和...
The json module: JavaScript Object Notation The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific ...
- ASP(Active Server Pages)使用VBScript或JavaScript作为脚本语言,易于学习,但性能相对较低。 - PHP(Hypertext Preprocessor)跨平台,语法简洁,支持多种数据库,并且完全免费,适合快速开发。 - JSP...
私聊涉及两个用户之间的消息传递,可以通过创建`private_chats`表来存储每条聊天记录,记录`sender_id`、`receiver_id`和`message`等信息。群聊则需要`groups`表来存储群组信息,`group_members`表记录用户与群组的...
// public members } } 模块图案结构 var Calculator = function ( ) { // private member var elDom = document . getElementById ( 'el' ) ; return { // expose public member add : function ( x ,
例如,类`Foo`的`public_ex`会被编码,而`private_ex`、`protected_ex`以及常量`ERROR_CODE`和方法`getErrorCode()`则不会。 四、`json_decode` `json_decode`函数将JSON字符串转换为PHP的数据结构。默认情况下,它...
- 支持多种脚本语言如 VBScript 和 JavaScript; - 可以在 Windows 平台上无缝集成 IIS; - 提供了强大的 COM 组件支持,便于创建复杂的应用程序。 - **缺点:** - 依赖于 Windows 和 IIS,跨平台性差; - 性能...
而标签“TypeScript”表明这个项目使用了TypeScript语言,它是JavaScript的一个超集,提供了静态类型检查和现代语言特性,如类、接口和泛型,极大地增强了代码的可维护性和可靠性。 在TypeScript中,面向对象的核心...