0

If I don't intend to inherit my controller class, is there any difference between defining an AngularJS Controller's methods as prototype methods over instance methods?

Coming from a C# background, I'm more comfortable with defining prototype methods. I also like keeping the constructor short.

However, most examples I see for AngularJS define methods at the instance level.

module Test {
    angular
        .module("app")
        .controller("controller",
        ["$scope", MyController]);

    export class MyController {

        public Function1: () => {}

        constructor(private $scope: IMyScope) {
            //instance
            this.Function1() = () => {
                alert("Hello 1!");
            }
        }
    }

    //prototype
    public Function2(): void {
        alert("Hello 2!");
    }

}

1 Answer 1

1

If I don't intend to inherit my controller class, is there any difference between defining an AngularJS Controller's methods as prototype methods over instance methods?

Instance methods come with a performance impact. Other than that it's perfectly fine to use them. I just use prototype members.

This video explains why you might want to use instance methods : https://m.youtube.com/watch?v=tvocUcbCupA (mainly to capture this)

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.