Posts

Showing posts with the label privileged

Natural JavaScript private methods

I've never seen this technique yet, but basically it allows us to create private methods, without privileged , and in an way that does not allow subclasses to inherit them ... does it sound interesting? ;) As we can read in my JavaScript Prototypal Inheritance for Classical Emulation documentation, there is a way to easily create private methods without usage of privileged . The advantage of this way is, as explained in my doc, is that JavaScript interpreter does not have to create many functions for each declared instance . Here there is a basic example: // our constructor function Person(name, age){ this.name = name; this.age = age; }; // prototype assignment Person.prototype = (function(){ // we have a scope for private stuff // created once and not for every instance function toString(){ return this.name + " is " + this.age; }; // create the prototype and return them return { // never forget the constructor ... ...