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官网下载,安全起见. 并与新建的两个文件部署如图 ...
随机推荐
- KVM中的网络简介(qemu-kvm)
emu-kvm主要向客户机提供了如下4种不同模式的网络: 1)基于网桥(bridge)的虚拟网卡 2)基于NAT(Network Addresss Translation)的虚拟网络 3)QEMU内置 ...
- Mac下复制粘贴的快捷键是什么?随记
刚从window换成Mac OS系统的用户对于一些常用的快捷键一定非常的不习惯,“mac复制粘贴快捷键是什么?”这一简单的问题相信很多刚刚从Windows平台转到Mac平台的用户会问到的问题,因为Ma ...
- 如何实时查看mysql当前连接数?
1.查看当前所有连接的详细资料: ./mysqladmin -uadmin -p -h10.140.1.1 processlist2.只查看当前连接数(Threads就是连接数.): ./mysqla ...
- 【06】Firebug记录Javascript日志
Firebug记录Javascript日志 你可以使用Firebug来生成日志. 这有助于我们调试web页面并发现页面的错误. 在Firefox浏览器中执行以下代码: <!DOCTYPE HTM ...
- 71.mybatis 如何获取插入的id【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 在之前的文章已经讲过spring boot集成mybatis了,但是忘记说一个很重要的知识点了,那就是获取获取主键id,这篇文章补充下,sprin ...
- .net对象的生命周期
阅读了文章:.NET对象生命周期小结 文章分多个部分,第一部分:介绍了,创建对象时,内存的分配,对象真正被创建,以及经历各阶段垃圾回收的过程. 第二部分,介绍了Finalize与Dispsose方法.
- [Go]GOPATH相关知识点
在成功安装好Go之后,执行命令 go env 就可以看到有关go的一些环境变量,其中比较关键的是GOROOT.GOPATH和 GOBIN 1.设置GOPATH环境变量有什么意义? GOPATH是指:指 ...
- numpy模块
NumPy简介: NumPy 是高性能科学计算和数据分析的基础包:它是pandas等其他工具的基础. NumPy的主要功能: 1. ndarray,一个多维数组结构,高效且节省空间 (最主要的功能) ...
- react.js 渲染一个列表的实例
//引入模块 import React,{Component} from 'react'; import ReactDOM from 'react-dom'; //定义一个要渲染的数组 let use ...
- 【ZJOI2017 Round1练习】D2T3 counter(线段树)
题意: 思路: 预处理出b[i]代表i位置之前比a[i]小的数的个数 以每个数为结尾的组数是线段树中(1,a[i]-1) 对于a[i]换到最后,相当于线段树中(a[i]+1,n)-- 交换后b[i]又 ...