Home > Web Front-end > JS Tutorial > What is the use of javascript privileged methods?

What is the use of javascript privileged methods?

青灯夜游
Release: 2023-01-05 16:11:00
Original
2255 people have browsed it

In JavaScript, privileged methods refer to those methods that allow users to access them as public methods while viewing and processing private variables. Function: Public access outside the constructor (limited to instantiated objects), and also able to access private members and methods; access to private properties or methods by public methods can be controlled through privileged methods.

What is the use of javascript privileged methods?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Privileged methods:

refers to those methods that allow users to access them as public methods while viewing and processing private variables.

Points:

1. A privileged method is a method

2. A privileged method can access private variables

Define a privileged method

The method defined through the this keyword inside the constructor can be called by the instantiated object inheritance.

var Student = function(name) {
var _name = name; //私有属性
//特权方法
this.getName = function() {
return _name;
};
this.setName = function(name) {
_name = name;
};
};
var s1 = new Student('zhangsan');
s1.getName(); //zhangsan
Copy after login

The role of privileged methods

Privileged methods can be publicly accessed outside the constructor (limited to instantiated objects), and can also access private members and methods. Therefore, the interface used as an object or constructor is most suitable. Through privileged methods, we can control the access of public methods to private properties or methods. There are many applications in the extension of JS framework.

The difference between privileged methods and public methods

Same points:

1. Both can be publicly accessed outside the constructor.

2. Public properties can be accessed.

Differences: There are 2 points

1. Each instance must have a copy of the privileged method (except in singletons) When used outside, memory needs to be considered), while public methods are shared by all instances

//创建Student对象实例
var s1 = new Student('zhangsan');
var s2 = new Student('lisi');
//两实例的特权方法的引用不相同, 说明在对象实例化的时特权方法被重新创建
console.log(s1.getName === s2.getName); //false
Copy after login

2. Privileged methods can access private properties and methods, but public methods cannot.

//为Student创建公有方法
//公有方法不能访问私有属性
Student.prototype.myMethod = function() {
console.log(_name); //ReferenceError: _name is not defined
};
s1.myMethod();
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What is the use of javascript privileged methods?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template