Skip to main content

Private Members in JavaScript

Popularity Report

Total Popularity Score: 0

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Rank

Related Lists

Bookmark History

Saved by 54 people (-20 private), first by anonymouse user on 2006-05-23


Public Comment

on 2006-12-20 by eliazar

Good intro to Javascript objectness.

Public Sticky notes

JavaScript is the world's most misunderstood programming language. Some believe that it lacks the property of information hiding because objects cannot have private instance variables and methods. But this is a misunderstanding. JavaScript objects can have private members. Here's how

Highlighted by zemoxian

Private methods are inner functions of the constructor

Highlighted by ratbeard

Private methods cannot be called by public methods. To make private methods useful, we need to introduce a privileged method.

Highlighted by matteosp

A privileged method is able to access the private variables and methods, and is itself accessible to the public methods and the outside. It is possible to delete or replace a privileged method, but it is not possible to alter it, or to force it to give up its secrets.

Highlighted by matteosp

A privileged method is able to access the private variables and methods, and is itself accessible to the public methods and the outside. It is possible to delete or replace a privileged method, but it is not possible to alter it, or to force it to give up its secrets.

Highlighted by ratbeard

This pattern of public, private, and privileged members is possible because JavaScript has closures. What this means is that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned. This is an extremely powerful property of the language. There is no book currently available on JavaScript programming that shows how to exploit it. Most don't even mention it.

Highlighted by matteosp

an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned. This is an extremely powerful property of the language.

Highlighted by ratbeard