5

I'm writing my first new Angularjs project.

I need to add my custom method (say, addHours()), to a javascript class, say Date().
For completeness, this is my (working) method:

Date.prototype.addHours = function(h) {
  this.setHours(this.getHours() + parseInt(h));
  return this;
}

Which is the recommended way to do it? Where to put the prototype functions, so they are available through all my project? Should I better create a new Date class? Should I better define an angular service?

1 Answer 1

2

Maybe this article can give you some insight.

Additionally, you could just include that snippet in a JS file on your index.html which would make all Date objects in your application have addHours. I tend to do this for any standard library 'enhancements' so I can easily identify where the standard library objects deviate.

Update:

A concrete example following along with the article, for the most part:

yourApp.factory( 'MyDate', function() {

    //Constructor
    function MyDate( date ) {
        this.date = date; //Wrapping a JS date object.
        //Probably want to add some safety here to make sure it IS a date obj
    }

    MyDate.prototype.addHours( h ) {
        this.date.setHours( this.date.getHours() + parseInt(h) );
    }

    return MyDate;
});

yourApp.controller( 'MyDateController', function( $scope, MyDate ) {

   $scope.someMethod = function(h) {
       //Some trigger, UI or what not
       $scope.myDate = new MyDate( new Date() );
       $scope.myDate.addHours(h);
       //Do something with the new date
   };
});
Sign up to request clarification or add additional context in comments.

17 Comments

Thanks! Unfortunately the article does not explain how to add a prototype to a javascript class like Date() (or perhaps I did not understand how to do it... :-(). About including a js snippet file to my project, yes, that's my current solution, but I would prefer a more 'angular' one... :-) (and more grunt-integrated...).
Updated my answer, hope it helps! And to answer where you put it, wherever you think it's reasonable to put your common-use factories. If it's a small application, next to your app declaration is probably fine.
Thanks! The code makes perfectly sense... Altough the new myClass(new Class()) completely misses one of the prototyping best features: to add a behavior to an existing class... I suppose I will fall back - as you suggested - on a pure javascript snippet sourced from my index.html... Not so angular, but it works... :-) I'll wait some hours more before accepting your answer, but I suppose it's the only possible solution... :-(
Yes, you could just use a regular function and not a prototype. DateTime() isn't. DateTime(obj) is though. Either pass in a parameter or create a constructor for DateTime() with no params.
Ahah! So I looked back over the article's example and missed a critical line. See my updated answer. Note the added return in the factory.
|

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.