简体   繁体   中英

How to declare private function in inherited javascript class

I really like javascript class inheritance described in this articale. Example:

var Person = Class.extend({

  init: function(isDancing){
    this.dancing = isDancing;  
  },  
  dance: function(){
    return this.dancing;
  }

});

In this case dance is public function. I want to declare private function "walk". But how and where? I`m sure it is easy for most of you.

Thank you.

This is how you can make private functions, but you lose the ability to prototype:

var Person = Class.extend((function () {
    var dancing;

    // private functions
    function dance () {
        return dancing;
    }

    // public functions
    return {
        init: function(isDancing) {
            dancing = isDancing;  
        }
    }
}())); // <-- immediate invocation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM