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官网下载,安全起见. 并与新建的两个文件部署如图 ...
随机推荐
- 局域网虚拟机端口映射访问apache
如果我们在虚拟机内搭建好服务器后,希望可以在局域网内的设备上都能访问到这个虚拟服务器,就可以参照以下步骤来操作.其中包括了很多遇到的坑.先说说我的环境是 宿主机:windows 8.1 虚拟机:vmw ...
- HDU1213最简单的并查集问题
题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1213 #include<iostream> using namespace std; #d ...
- [转]SQL SERVER数据库还原的方法
SQL SERVER数据库还原的方法 在SQL SERVER 2005下还原数据库 1.新建数据库A,右键还原数据库,此时目标数据库为A,选择备份 文件B_db_201311040200.BAK,还原 ...
- Codeforces947D. Picking Strings
$n \leq 100000,m \leq 100000$,给长度$n$的字符串$s$和$m$的字符串$t$,只含ABC.定义串$a$可以经过任意次如下操作变成其他串. 现在$q \leq 10000 ...
- ES6__变量的解构赋值
/* 变量的解构赋值 */ /* 基本概念 : 本质上就是一种匹配模式,只要等号两边的模式相同,那么左边的变量就可以被赋予对应的值. 结构赋值主要分为: 1. 数组的解构赋值 2. 对象的结构赋值 3 ...
- 出现 Assigning the return value of new by reference is deprecated in xxxx &&“Warning: Call-time pass-by-reference has been deprecated”怎么办?
自从php5.3,越来越多的人会遇到“Assigning the return value of new by reference is deprecated in xxxx”这样的提示,尤其是在国外 ...
- django学习之- json序列化
序列化操作 - Errordict - 自定义Encoder - django的模块可以直接序列化 第一种: from django.core import serializers # 通过这个模块对 ...
- PAT (Advanced Level) 1038. Recover the Smallest Number (30)
注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...
- Fibonacci--poj3070(矩阵快速幂)
http://poj.org/problem?id=3070 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn ...
- eclipse菜单字体乱码的解决
方法一: 这个跟活动控制台代码页有关. 如果要更改为 UTF-8,则需要运行 chcp 命令: chcp 65001 有时新安装的系统可能在运行一些中文软件时显示错乱,可通过控制面板修改系统区域来管理 ...