Posts

Showing posts with the label private

JavaScript Protected Properties

Imagine this simple piece of code: var obj = { _name: "protected", gimmeName: function () { return this._name; } }; obj.gimmeName(); // "protected" obj._name; // throws new Errror now, imagine I have the simplest solution ever, compatible with both ES5 and ES3 genric implementations ... now please keep reading to know a bit of background :) The JS Meaning Of Protected JS developers know that in JavaScript properties are always public and that it is possible to attach/detach properties in any moment to a generic object. Regardless this fact, many libraries consider protected, by naming convention, all those properties whose name start with "_" . You Are Doing It Wrong First of all I keep saying that is a common error trying to bring classical Object Oriented concepts into a completely different language as JavaScript is, but it does not matter ... we want protected stuff, right? Due dynamic nature, the common protected meaning is point...

a new Relator object plus unshared private variables

This is a Robert Nyman 's post dedicated reply, about JavaScript inheritance and alternatives and private variables . First of all, I would like to say thanks to Robert for both interesting articles He is writing about JS inheritance, and a link to a personal comment which aim was to bring there my "old" documentation about classical JavaScript inheritance and usage of prototype, closures, and public, privileged, or private, scope when we create a constructor. About Last Rob's post talk about private variables, describing them as shared, if present outside the constructor, and valid only for singleton instances. This is true, and could cause a lot of headache if we are not truly understanding closures and prototype shared methods behaviour, but there is a way to use this peculiarity about shared private variables to create dedicated private variables. Before I will write about it, let's look into a generic shared private variable example: Click = function(){ ...

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 ... ...

My 5 cents about JavaScript Prototypal Inheritance

Few days ago I read an interesting post about Simple JavaScript Inheritance . The most hilarious thing is that prototypal inheritance is truly simple, as John wrote, but there are still a lot of developers that do not probably understand perfectly them. In this link you can find an " all in one " page about JavaScript prototypal inheritcance and classical emulation. As I wrote at the end of that page, please do not hesitate to correct me if there is something wrong, or please ask me more details if there is something that is not so clear. Sorry for my not perfect yet English, and have fun with JavaScript. (happy easter too!)