博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A Simple Example About Privileged Methods in JavaScript
阅读量:4606 次
发布时间:2019-06-09

本文共 2600 字,大约阅读时间需要 8 分钟。

Douglas Crockford classified the "class methods" in JavaScript into three types:
private,
privileged and
public.
Public methods have an obvious meaning: they can be accessed by the public.
Private methods' meaning are also clear: they cannot be accessed by the public.
So what are
privileged methods? To understand that, we should have a short review of how we implement public and private methods in JavaScript first.
Recall that, unlike languages like C++, JavaScript does not have "class". Moreover, it does not have
access specifiers like
public,
protected and
private. Lack of these features make the creation of private and public methods less clear than it should be.
To write a
public method in JavaScript, we make use of the
.prototype property of the constructor function. For example:
function FootballPlayer(){}; // declare a functionFootballPlayer.prototype.kick = function(){ alert("kick!"); }; // create a public method kick in .prototypevar Messi = new FootballPlayer();Messi.kick();
From my , you have learnt that
FootballPlayer.prototype is an object that is built automatically when the function is created. Object constructed with the FootballPlayer, Messi, search through his
__proto__ chain when we tell him to kick. Obviously
FootballPlayer.prototype is on the chain so Messi knows how to kick. All objects created by the
constructor function share the same
FootballPlayer.prototype so they all can invoke the
public method kick!
Private methods are kinda tricky. We declare private variable in a
constructor using the keyword
var:
function man() {	var wealth;	var bath = function(){};	function kiss(){}}
In this case, none of wealth, bath or kiss are accessible outsite the function man. Even man's
public methods
cannot access them.
However, the
privileged methods can access the
private members due to the existence of
closures. They are very useful as they can act as a bridge between the outsite world and the inner status of the object.
Consider the following example:
var func = function(a,b) {    this.a = a;    this.getB = function(){return b}; // a privileged method    this.setB = function(n){b=n;}; // another privileged method    var b = b;};func.prototype.getB2 = function(){return b*2}; // public methodvar obj = new func(1,2);alert(obj.getB()); // privileged method can access private balert(obj.getB2()); // error: public method cannot access private bobj.setB(11);alert(obj.getB());
So actually we can create
privileged methods easily by using the keyword
this!
Read More:
by RedcapCoder
 by Douglas Crockford

转载于:https://www.cnblogs.com/blfbuaa/p/7200174.html

你可能感兴趣的文章
JavaScript面试题
查看>>
[转帖]架构师眼中的高并发架构
查看>>
ios的一些开源资源
查看>>
HTTP 错误 500.21 - Internal Server Error 解决方案
查看>>
Bucks sign Sanders to $44 million extension
查看>>
【PHP】Windows下配置用mail()发送邮件
查看>>
Nhibernate和EF的区别
查看>>
基于java spring框架开发部标1078视频监控平台精华文章索引
查看>>
人类简史
查看>>
java 设计模式学习
查看>>
【Python使用】使用pip安装卸载Python包(含离线安装Python包)未完成???
查看>>
一语道破项目管理知识体系五大过程组
查看>>
C# 备份、还原、拷贝远程文件夹
查看>>
在windows环境下运行compass文件出现的错误提示解决方案
查看>>
CSS常用样式--font
查看>>
恩如氏--蜗牛精华补水蚕丝面膜
查看>>
大工具-收藏
查看>>
codevs3027 线段覆盖 2
查看>>
markdown
查看>>
【leetcode】107-Binary Tree Level Order Traversal II
查看>>