简体   繁体   中英

How to make inner function to return inner variable in javascript

Forgive me for asking this simple question.

I want to get the inner variable through method.In java, I can achieve it as below code.

class MyClass{
    String words = "hello";     
    MyClass(){
        // constructor
    }
    String getWords(){
        return this.words;
    }
}

Then, I can get the words (inner variable) by calling the method getWords() .

MyClass myClass = new MyClass(); // object instance
System.out.println(myClass.getWords()); // get the words

My question is, how can i achieve such thing in javascript.

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

With pure javascript :

var MyClass = function () {
    this.words = 'hello';
};

MyClass.prototype.getWords = function () {
    return this.words;
};

var myClass = new MyClass();
alert(myClass.getWords());

In that case words is a public property, so you can access it directly like so :

alert(myClass.words);

If you want to make it private as described around, note that :

  • getWords will not take part in class inheritance
  • it's a dirty choice in terms of memory usage since getWords is defined each time MyClass is instanciated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM