0

I recently updated my PHP library to use the dependency injection pattern. Wrather than instantiating objects in my classes, I instantiate them in a class called ObjectMaker and pass the objects in through object "getter" functions.

The only point I can see to doing this is for isolating faults caused by instantiation.

But since I am just one guy, and I'm not doing any unit testing I don't see any point to updating my JavaScript to use the dependency injection pattern.

These dependencies just don't cause me any problems.

Can I not used it and still call my JavaScript professional?

I'm not going to see any performance gains by using it, just more abstract code ( objects creating objects ) - I favor a simple object model with not a lot of "pattern" use. This is style preference correct?

Question?

Can I not use the dependency injection pattern and still call my JavaScript professional level?

/**
 *Control
 */

var Control = ( function () 
{
    var Control = function ( type )
    {
        this.TIME = 4000;
        this.form_element = document.getElementById( type ), 
        this.response_element = document.getElementById( type + '_response' );
        this.text_object = new Text( this.form_element ),
        this.message_object = new Message( this.response_element ),
        this.effects_object= new Effects( this.response_element );
    };
    Control.prototype.invoke = function( ) 
    {
        if( Global.validate_input_on === 1 )
        {
            if( !this.text_object.checkEmpty() ) 
            {
                this.message_object.display( 'empty' );
                this.effects_object.fade( 'down', this.TIME );
                return false;
            }
            if( type === 'signup' && !this.text_object.checkPattern( 'name' ) ) 
            {
                this.message_object.display( 'name' );
                this.effects_object.fade( 'down', this.TIME );
                return false;
            }
            if( !this.text_object.checkPattern( 'email' ) ) 
            {
                this.message_object.display( 'email' );
                this.effects_object.fade( 'down', this.TIME );
                return false;
            }
            if( !this.text_object.checkPattern( 'pass' ) ) 
            {
                this.message_object.display( 'pass' );
                this.effects_object.fade( 'down', this.TIME );
                return false;
           }
        }
        var response_element = this.response_element;
        AjaxNew.repeatUse( ajaxSerialize( this.form_element ) + '&ajax_type=' + type + '_control', function( server_response_text ) { ajaxType( server_response_text, this.response_element, 'respond' ); } );
    };
    Control.in = function()
    {
        new Control( 'signin' ).invoke();
    };
    Control.up = function()
    {
        new Control( 'signup' ).invoke();
    };
    Control.out = function()
    {
        AjaxNew.repeatUse( '&ajax_type=ControlSignOut', function( server_response_text ) { ajaxType( server_response_text, 0, 'simple' ); } );
    };
    Control.try = function()
    {
        AjaxNew.repeatUse( '&ajax_type=ControlTryIt', function( server_response_text ) { ajaxType( server_response_text, 0, 'simple' ); } );
    };
    return Control;
} () );

2 Answers2

2

You might risk not being the cool kid on the block with the other devs, but it doesn't mean your code is less professional. Use what works best for your project. If it's overkill for what you are trying to do, don't use it.

1

The only point I can see to doing this is for isolating faults caused by instantiation.

The purpose of Dependency Injection is to promote loose coupling. Whether you need this in your JS code or not I would say largely depends on the scope of your JS code; i.e. are you writing a JS framework, or manipulating a few divs?

Will a web development company find your work professional? It depends. Are you gratuitously adding DI to your code so that you can look cool, or does it have some genuine reason to be there? A professional web company will know the difference.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501