Your words instance member is package private. It's not public, but it's not private either; any other class in the same package can access it. That's tricky to achieve in JavaScript.
If you wanted it to be private, the classic pattern for that in JavaScript looks like this:
function MyClass() {
var words = "hello";
this.getWords = function() {
return words;
};
}
var instance = new MyClass();
console.log(instance.getWords());
Note that every object created via new MyClass will get its own copy of the getWords function. words is just a variable in the constructor; the getWords function closes over it. Crockford discusses this in his article Private Members in JavaScript.
For something closer to package private, or just to avoid recreating that function for every instance, you'd want the pattern being planned for ES6 — a pattern that can be very nearly implemented in ES5 (or even ES3):
var MyClass;
var AnotherPackageClass;
(function() {
// Everything within this scoping function is the "package"
var wordsKey = new Name(); // ES6, but see article below for an ES3/ES5 option
MyClass = function() {
this[wordsKey] = "hello";
};
MyClass.prototype.getWords = function() {
return this[wordsKey];
};
AnotherPackageClass = function() {
};
AnotherPackageClass.prototype.getWordsFrom(o) {
return p[wordsKey];
};
))();
Note the new Name call. In ES6, that will create something called a private name object. It's not a string, but it can be used like one for accessing properties. Since the property doesn't have a name, it can't be accessed without the key. This allows us to create truly private properties.
I discuss this pattern and how you can get very near it in ES5 — and even ES3 — in this article: Private Properties in ES6 -- and ES3, and ES5
Both patterns above rely on closures, in different ways. My article on them may be useful if they're unfamiliar to you: Closures are not complicated
getWords()should befunctionnotstringjavascript?