0

I want to try object oriented js, and I want to know which approach is better out of the two I am trying and why it is better please explain, If there are any other better approach please suggest me.

// Approch 1
var thisPage;
abc.forgotPassword = {
    init: function() {
        'use strict';
        thisPage.forgotPasswordFunctionality();
    },
    forgotPasswordFunctionality: function() {
        //some code
    }
};
$(function() {
    thisPage = abc.forgotPassword;
    thisPage.init();
});


// Approch 2
abc.forgotPassword = {
    init: function() {
        'use strict';
        abc.forgotPassword.forgotPasswordFunctionality();
    },
    forgotPasswordFunctionality: function() {
        //some code
    }
};
$(function() {
    abc.forgotPassword.init();
});
1
  • The second one because it doesn't depend on an assignment outside the function (i.e. thisPage = abc.forgotPassword;). Commented Oct 24, 2014 at 16:01

1 Answer 1

1

This isn't really object-oriented Javascript- you're just storing functions in an object.

If you want to make it more object-oriented, do something like this:

abc.ForgotPasswordHelper = function() {
    this.init = function() { ... }
    this.forgotPasswordFunctionality = function() { ... }
}

var helper = new abc.ForgotPasswordHelper();
helper.init();
Sign up to request clarification or add additional context in comments.

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.