Private method: The private method itself can access all properties inside the class, that is, private properties and public properties. But private methods cannot be called outside the class.
Private method writing:
function myClass () {
var private_attribute = initial_value;
function private_method () { }
var private_method2 = function () { }
}
Instance showpet() is a Private method
var pet=function(){ var temp="" //Private variables can only be accessed within the function or object scope
function showpet(){
alert("123")
}
showpet();// Private methods can be used within function scope.
}
showpet();//There will be an error
pet.showpet()//It still cannot be called like this
var Penguin=new pet() //Instantiate a pet object
Penguin.showpet()//Sorry, I still can’t let you call it like this.
Public methods:
1. Public methods can be called outside the class,
2. But they cannot access the class Private property.
3. Public methods must be added inside or outside the class through the prototype attribute of the class.
Public method writing:
function myClass () {
this.public_attribute = initial_value;
this.prototype.public_method = function () { }
}
myClass.prototype.public_attribute2 = initial_value;
myClass.prototype .public_method2 = function () { }
Example:
var pet=function(){
function showname(){//Private method
alert(this.name)
}
this.show=function(){ //If you don't understand here, please note that this method will be introduced below.
Showname();
}
}
pet.prototype.setname=function(str){
name=str;
}
var Penguin=new pet()
Penguin.setname("Penguin");//Add the name value of the instance to Penguin
Penguin.show(); //Pop up Penguin
Penguin.setname("wind");//Add an instance The name value is wind
Penguin.show(); // Pop up wind
Privileged methods:
1. Privileged methods can be called outside the class Called,
2. But it can access the private properties of the class, and it can also access the public properties of the class. It can be reluctantly considered to be a special public method.
3. But it is different from the way of declaration and definition of the public method above. Privileged methods must be declared inside the class.
Privileged method writing:
function myClass () {
this.privileged_method = function () { }
}
Instance
var pet=function(){
function showname(){//Private method
alert(this.name)
}
this.show=function(){//Define a privileged method by using the this keyword.
showname(); //Access private methods in privileged methods;
}
}
pet.prototype.setname=function(str){
name=str;
}
var Penguin=new pet(); //Instantiate a pet object
Penguin.setname("Penguin"); //Call public methods to modify
Penguin.show(); //Call privileged methods Access the private method and pop up name
다음은 제가 읽은 책과 결합하여 위의 내용을 연구하여 제가 이해한 것입니다. Public, Private, Privilege에 대한 이해는 다음과 같습니다.
Public 메소드: 이 클래스를 통해 인스턴스화된 모든 객체가 갖고 있거나 사용할 수 있는 메소드입니다. 일반적으로 공통 메소드는 "프로토타입 객체"에 배치됩니다. 생성자에 배치하면 공통 메소드가 반복적으로 생성됩니다.
Private 메서드: 외부에서 호출할 수 없습니다.
특권 메서드: 클로저 원칙이 사용됩니다. 즉, 내부 함수는 범위 체인을 통해 외부 함수의 변수 개체(즉, 클래스의 프라이빗 변수 및 프라이빗 메서드)에 액세스할 수 있습니다. (스코프 체인, 클로저, 변수 객체. 이 세 가지는 "Javascript 고급 프로그래밍"에서 설명됩니다.)