0

I have a method loadSet which creates elements with datas from the localstorage, and this should be run on page load i am calling it via

ReminderSet.prototype.loadSet(); // works fine

My question is, is there any other way to call a method that don't need a reference to an object instance? like person1.loadSet(); or should i abandon this and make it as a regular function?

ReminderSet.prototype.loadSet = function() {
    var keys = Object.keys(localStorage),
        i = 0,
        key,
        array;

    for (; key = keys[i]; i++) {
        const setId = localStorage.getItem(key);
        array = JSON.parse(setId); //parse and store key values
        let array_index = 0;
        //Re-create the reminders and set their properties//
        $reminderSection.append($('<div/>').addClass('set').attr('id', key) //Set the ID                      
            .append($('<div/>').addClass('set-title').append($('<h1>').attr('contenteditable', 'true').text(array[array_index].set_title)), //Index is always at 0//
                $('<div/>').addClass('create-reminder-control').append($('<button>').addClass('add-new-reminder').text("+ add new"), $('<input>').addClass('create-reminder-value').attr({ type: "text", placeholder: "get something done" })), $('<div/>').addClass('reminder-lists'), $('<div/>').addClass('save-control').append($('<button>').addClass('save-reminder-button').text('Save'))))

        //Get our key values //
        for (; array_index < array.length; array_index++) {
            /*Select the element id */
            $("#" + key).children('.reminder-lists').append($('<div/>').addClass('a-reminder').attr('contenteditable', 'true').text(array[array_index].description).append($('<div/>').addClass('delete-reminder').text('x'))) //Get the reminders
        } //end

    }
};
1
  • 1
    If you need to call it without an instance, it probably shouldn't be an instance method. Define it on the object directly, not its prototype. That's basically a "static" function. Commented Oct 22, 2017 at 16:59

2 Answers 2

2

If loadSet doesn't need or use an instance, it doesn't make any sense for it to be on ReminderSet.prototype. Either make it a standalone function:

function loadSet() {
    // ...
}
// Call it like so:  loadSet();

...or a property on ReminderSet itself:

ReminderSet.loadSet = function() {
    // ...
};
// Call it like so:  ReminderSet.loadSet();

Only put functions on the object that a constructor's prototype property refers to if they need to use this (the instance).

Sign up to request clarification or add additional context in comments.

Comments

1

You can set the function directly as a property of the other ReminderSet:

ReminderSet.loadSet = function() {//etc.}

Then you can simply call: ReminderSet.loadSet()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.