Home  >  Article  >  Web Front-end  >  What are public methods and private methods in javascript

What are public methods and private methods in javascript

青灯夜游
青灯夜游Original
2022-02-07 14:50:372601browse

In JavaScript, public methods refer to methods that can be accessed and called from the outside; while private methods refer to methods that are declared in the constructor of an object and are invisible and inaccessible to the outside.

What are public methods and private methods in javascript

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

1: Public methods

Public methods are methods that can be accessed and called externally

// 对象中
var test1 = {
    name:'大白',
    getName:function(){
        console.log(this.name);
    }
}
//调用
test1.getName();//大白

// 构造函数中
function test2(name,age){
    this.name = name;
    this.age = age;
    //公有方法
    this.getName = function(){
        console.log(this.name);
    }
}
// 在原型中
test2.prototype.getAge = function(){
    console.log(this.age);
}
//调用
var test3 = new test2('小白',12);
test3.getName();//小白
test3.getAge();//12

2: Private methods and public methods

Privileged methods refer to public methods that have access to internal private properties and private methods ( Able to access private methods and private attribute methods are called privileged methods, which are also a type of public methods)

Private methods are declared in the constructor of the object and are not visible and accessible from the outside. Methods.

Use different ways to define private methods and privileged methods in different forms

In objectsWe use Object object expressions to create an object and add some properties and methods, Then call it directly in a static way. Such as Rest.getName();

Immediate execution functionThe private data of the object is placed in an anonymous function immediate execution expression (IIFE), which means that this function only exists when it is called The moment it is executed, it will be destroyed immediately

var yourObject = (function() {

 // 私有属性和方法
 return {
 // 公有方法和属性
 }
}) ();

This is the same as the previous definition of Rest, and can be accessed directly through yourObject. This kind of modular access is quite powerful.

var test4 = (function(){
    //私有属性
    var total = 10;
    // 私有方法
    var buy = function(){
        total--;
    }
    var get = function(){
        return total;
    }
    return {
        name:'小白白',
        getTotal:get,//使用了闭包的方式来简介使用内部私有变量
        buyfood:buy
    }
})();
test4.buyfood();
console.log(test4.name);//小白白
console.log(test4.getTotal());//9

Uses closure to indirectly use internal private variables

It is convenient to define private properties and methods in the constructor, we don’t If you need to use a closure, you can initialize the data when calling

// 构造函数中

function test5(name) {
 // 私有属性
 var total = 10;

 // 公有属性
 this.name = name;

 // 私有方法
 function _buyFood() {
    total--;
 }

 // 特权方法,才能访问私有的属性和私有的方法
 this.buy = function() {
     _buyFood();
 }

 this.getTotal = function() {
    return total;
 }
}

// 公有方法, 注意这里不能访问私有成员_total
test5.prototype.getName = function() {
    //console.log(_total); // Uncaught ReferenceError: _total is not defined
    return this.name;
}

var test6 = new test5('大小白');
console.log(test6.getName()); // '大小白'
test6.buy();
console.log(test6.getTotal()); // 9

Used in conjunction with

Using the constructor method, you can pass in some initialized data , but private member attributes cannot be accessed in public methods. If there are many public methods that need to access private data, we will write them all in privileged methods, which will eventually bring many unnecessary methods to each instance.

var test7 = (function(){
    // 私有属性
    var total = 10;

    // 私有方法
    function buyFood(){
        total--;
    }
    // 构造函数
    function test7(name){
        this.name = name;
        this.getTotal = function(){
            return total;
        }
    }
    // 公有方法  这里不是test7内部的私有
    test7.prototype.buy = function(){
        console.log(total);
        buyFood();
    }
    test7.prototype.getName = function(){
        return this.name;
    }
    return test7;
})();
var test0 = new test7('大大白');
console.log(test0.getName());//大大白
test0.buy();//10
console.log(test0.getTotal());//9

【Related recommendations: javascript learning tutorial

The above is the detailed content of What are public methods and private methods in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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