A Simple Example About Privileged Methods in JavaScript
private, privileged and public.
privileged methods? To understand that, we should have a short review of how we implement public and private methods in JavaScript first.
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.
public method in JavaScript, we make use of the .prototype property of the constructor function. For example:
function FootballPlayer(){}; // declare a function
FootballPlayer.prototype.kick = function(){ alert("kick!"); }; // create a public method kick in .prototype
var Messi = new FootballPlayer();
Messi.kick();
From my previous article, 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!
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.
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.
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 method
var obj = new func(1,2);
alert(obj.getB()); // privileged method can access private b
alert(obj.getB2()); // error: public method cannot access private b
obj.setB(11);
alert(obj.getB());
So actually we can create privileged methods easily by using the keyword
this!
A Simple Example About Privileged Methods in JavaScript的更多相关文章
- Static and Instance Methods in JavaScript
class.method/instance method https://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-jav ...
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- Javascript——概述 && 继承 && 复用 && 私有成员 && 构造函数
原文链接:A re-introduction to JavaScript (JS tutorial) Why a re-introduction? Because JavaScript is noto ...
- Classical Inheritance in JavaScript
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance in ...
- Private Members in JavaScript
Private Members in JavaScript Douglas Crockford www.crockford.com JavaScript is the world's most mis ...
- [转]Running JavaScript in an iOS application with JavaScriptCore
原文:https://www.infinum.co/the-capsized-eight/articles/running-javascript-in-an-ios-application-with- ...
- OOP in JS Public/Private Variables and Methods
Summary private variables are declared with the 'var' keyword inside the object, and can only be acc ...
- JavaScript中Object的总结
基于原型继承,动态对象扩展,闭包,JavaScript已经成为当今世界上最灵活和富有表现力的编程语言之一. 这里有一个很重要的概念需要特别指出:在JavaScript中,包括所有的函数,数组,键值对和 ...
- 在JavaScript中使用json.js:Ajax项目之POST请求(异步)
经常在百度搜索框输入一部分关键词后,弹出候选关键热词.现在我们就用Ajax技术来实现这一功能. 一.下载json.js文件 百度搜一下,最好到json官网下载,安全起见. 并与新建的两个文件部署如图 ...
随机推荐
- MySQL中的事务日志
一.事务日志的作用 事务日志在保证事务的特性的同时,提高事务的执行效率 二.事务日志的工作原理 使用事务日志时,存储引擎修改了表的数据时只需要修改其内存拷贝. 然后再将修改行为记录到持久在硬盘上的事务 ...
- openmediavault 4.1.3 插件开发
参考网址:https://forum.openmediavault.... 创建应用GUI 创建应用目录:/var/www/openmediavault/js/omv/module/admin/ser ...
- python基础知识01-数据类型和序列类型
%,取余 //,取整,向下取整,5//2 = 2. 一.变量类型 1.变量名不能以数字开头,不能纯数字,不要用汉字,可以用下划线开头 2.数值类型(int,float,bool,complex) ...
- 【51nod 1791】 合法括号子段
有一个括号序列,现在要计算一下它有多少非空子段是合法括号序列. 合法括号序列的定义是: 1.空序列是合法括号序列. 2.如果S是合法括号序列,那么(S)是合法括号序列. 3.如果A和B都是合法括号序列 ...
- Linux使用yum命令安装软件时,连接不了网路报错:ERROR 6 - "Couldn't resolve host 'mirrorlist.centos.org'"
错误: 解决方案: 在/etc/sysconfig/network-scripts/ifcfg-eth0文件中配置DNS信息: vim /etc/sysconfig/network-scripts/i ...
- PyMySQL操作mysql数据库(py3必学)
一,安装PyMySQL Python是编程语言,MySQL是数据库,它们是两种不同的技术:要想使Python操作MySQL数据库需要使用驱动.这里选用PyMySQL驱动. 安装方式还是使用pip命令. ...
- Linux 磁盘测速
读: time dd if=/dev/zero of=/test.dbf bs=8k count=1000000 写: time dd if=/dev/zero of=/var/test bs=8k ...
- POJ2455 Secret Milking Machine【二分,最大流】
题目大意:N个点P条边,令存在T条从1到N的路径,求路径上的边权的最大值最小为多少 思路:做了好多二分+最大流的题了,思路很好出 二分出最大边权后建图,跑dinic 问题是....这题是卡常数的好题! ...
- sequence(bzoj 1367)
Description Input Output 一个整数R Sample Input 794820141518 Sample Output 13 HINT 所求的Z序列为6,7,8,13,14,15 ...
- web移动端小tip,box-flex
1,移动端页面 最重要的标签: <meta content="width=device-width,initial-scale=1.0,minimum-scale=1,maximum- ...