JavaScript private methods

前端 未结 30 1491
-上瘾入骨i
-上瘾入骨i 2020-11-22 08:16

To make a JavaScript class with a public method I\'d do something like:

function Restaurant() {}

Restaurant.prototype.buy_food = function(){
   // something         


        
30条回答
  •  盖世英雄少女心
    2020-11-22 08:49

    Here is the class which I created to understand what Douglas Crockford's has suggested in his site Private Members in JavaScript

    function Employee(id, name) { //Constructor
        //Public member variables
        this.id = id;
        this.name = name;
        //Private member variables
        var fName;
        var lName;
        var that = this;
        //By convention, we create a private variable 'that'. This is used to     
        //make the object available to the private methods. 
    
        //Private function
        function setFName(pfname) {
            fName = pfname;
            alert('setFName called');
        }
        //Privileged function
        this.setLName = function (plName, pfname) {
            lName = plName;  //Has access to private variables
            setFName(pfname); //Has access to private function
            alert('setLName called ' + this.id); //Has access to member variables
        }
        //Another privileged member has access to both member variables and private variables
        //Note access of this.dataOfBirth created by public member setDateOfBirth
        this.toString = function () {
            return 'toString called ' + this.id + ' ' + this.name + ' ' + fName + ' ' + lName + ' ' + this.dataOfBirth; 
        }
    }
    //Public function has access to member variable and can create on too but does not have access to private variable
    Employee.prototype.setDateOfBirth = function (dob) {
        alert('setDateOfBirth called ' + this.id);
        this.dataOfBirth = dob;   //Creates new public member note this is accessed by toString
        //alert(fName); //Does not have access to private member
    }
    $(document).ready()
    {
        var employee = new Employee(5, 'Shyam'); //Create a new object and initialize it with constructor
        employee.setLName('Bhaskar', 'Ram');  //Call privileged function
        employee.setDateOfBirth('1/1/2000');  //Call public function
        employee.id = 9;                     //Set up member value
        //employee.setFName('Ram');  //can not call Private Privileged method
        alert(employee.toString());  //See the changed object
    
    }
    

提交回复
热议问题