JavaScript How to access public property from private and public functions in a JavaScript module? access public property in aspx page,access public property,access

I'm wondering how to access a public property from private and public functions in a JavaScript module?

For example, if this is my module:

var PersonModule = (function(){

    var sayHello = function(){
        alert("Hello " + name);
    }

    var privateChangeNameToBob = function(){
        this.name = "Bob";
    }

    var changeName = function(){
        privateChangeNameToBob();
    }

    return {
        name: "",
        sayHello: sayHello,
        changeName: changeName
    }   
})();

Neither sayHello or the changeName functions work correctly because I don't know how to access the public name property. Is there a way to do this? I've worked around it by using private variables with getters and setters, but I'm wondering if I can do it without them.

Answer:1

You need a reference to the result object:

var PersonModule = (function(){
    function sayHello() {
        alert("Hello " + person.name);
    }
    function privateChangeNameToBob() {
        person.name = "Bob";
    }
    function changeName() {
        privateChangeNameToBob();
    }

    const person = {
        name: "",
        sayHello,
        changeName
    };
    return person;
})();

Of course, given your singleton IIFE module, you could just as well refer to PersonModule.name directly.

Answer:2

In the way you're doing it the name variable that you're using as public it's not the same as the one in your sayHello method. You have to understand that this is a MODULE not a class that needs to be instantiated. The IIFE already instantiates it for you. To do what you want you can use the below model. Use self. in the public methods/vars and nothing in the private. Then it will all work.

var PersonModule = (function(){
    var self = {};

    var privateChangeNameToBob = function(){
        self.name = "Bob";
    };

    self.sayHello = function(){
        console.log("Hello " + self.name);
    };

    self.changeName = function(){
        privateChangeNameToBob();
    };

    self.name = "";
    
    return self;
})();


PersonModule.name = 'Test';
PersonModule.changeName();
PersonModule.sayHello();
Answer:3

You need to declare the property inside your closure. Check this example out to understand it better:

var ClassName = function(constructorParam) {
 
    var privateProperty = 'Im a private property';
    this.publicProperty = 'Im a public property';
 
    function privateMethod() {
        console.log('Private method');
    }
 
    this.publicMethod = function() {
        console.log('Public method');
        // We can call private properties
        console.log(privateProperty);
        // Either private methods
        privateMethod();
    }
 
}
 
var instance = new ClassName('Here goes something to constructor');
instance.publicMethod();
Answer:4

I have a maybe little problem. I use the swiper plugin and inside I have one select that is overrided by the jQuery UI select menu. <script type="text/javascript"> $(select).selectmenu() var ...

I have a maybe little problem. I use the swiper plugin and inside I have one select that is overrided by the jQuery UI select menu. <script type="text/javascript"> $(select).selectmenu() var ...

  1. jquery selectmenu set value
  2. jquery selectmenu on change
  3. jquery selectmenu refresh
  4. jquery selectmenu width
  5. jquery selectmenu select option
  6. jquery selectmenu get selected value
  7. jquery selectmenu multiple
  8. jquery selectmenu is not a function
  9. jquery selectmenu disable
  10. jquery selectmenu destroy
  11. goquery selection reset
  12. goquery selection size
  13. jquery selectmenu trigger change
  14. goquery selection change value
  15. jquery selectmenu widget
  16. jquery selectmenu change selected option
  17. goquery selection not working
  18. jquery select menu
  19. jquery select menu value
  20. jquery select menu item

I want to find the next element with a given class after the element which trigged the event, regardless of the position of the searched element. Let’s say I have multiple pairs of inputs. One with ...

I want to find the next element with a given class after the element which trigged the event, regardless of the position of the searched element. Let’s say I have multiple pairs of inputs. One with ...

  1. next element with class javascript
  2. next element with class jquery
  3. next element with class css
  4. next element with same class jquery
  5. next element with class
  6. next element with id jquery
  7. next element with css
  8. next element with javascript
  9. next element with jquery
  10. next element in list python
  11. next element in array javascript
  12. next element in array python
  13. next element in vector c++
  14. next element in list
  15. next element for loop python
  16. next element in xpath
  17. next element in array php
  18. next element in map c++
  19. next element in array java
  20. next element in foreach java

How can I highlight selected value from a multiple html select tag with data from the database? <select required name="PrevOptns" id="PrevOptns" size="7" multiple class="select_with_label_wide"&...

How can I highlight selected value from a multiple html select tag with data from the database? <select required name="PrevOptns" id="PrevOptns" size="7" multiple class="select_with_label_wide"&...

  1. html highlight value

I have a large object, and not each element within the object has the same keys or number of keys. The first element has all the keys, and this first element's values are what I want to actually USE ...

I have a large object, and not each element within the object has the same keys or number of keys. The first element has all the keys, and this first element's values are what I want to actually USE ...

  1. javascript write object to file
  2. javascript write object to console
  3. javascript write object to json file
  4. javascript write object as string
  5. javascript write object to html
  6. javascript write object to csv
  7. javascript write object to localstorage
  8. javascript write object to json
  9. javascript write object
  10. javascript write object properties
  11. javascript write object to cookie
  12. javascript write object to document
  13. javascript write all object properties
  14. javascript write out object
  15. javascript write json object as string
  16. javascript write json object to console
  17. node write javascript object to file
  18. javascript write out json object
  19. javascript write into object