ES5.1

var myobj = (function () {

    // private members

    var name = "my, oh my";

    // implement the public part

    return {

        getName: function () {

            return name;

        }

    };

}());

myobj.getName(); // "my, oh my"



메모리 아끼는법 

function Gadget() {

    // private member

    var name = 'iPod';

    // public function

    this.getName = function () {

        return name;

    };

}

Gadget.prototype = (function () {

    // private member

    var browser = "Mobile Webkit";

    // public prototype members

    return {

        getBrowser: function () {

            return browser;

        }

    };

}());

 

 

ES6 

class Gadget {

var name = 'iPod'

getName() {

return name; 

}



static getBrowser() {

  var browser = "Mobile Webkit";

  return browser;

  }

}

 

 

ES2021+

class Gadget {

  #name = 'iPod'

  getName() {

  return name; 

  }
}

 

참조: 

http://www.2ality.com/2016/01/private-data-classes.html

+ Recent posts