Tuesday, February 5, 2013

Understanding AngularJS's Concept about using Services the Javascript way

If you got lost on trying to understand AngularJS's concept about using services here's what the concept means in Javascript's terms with simple example;

=== begin code ====

var MyService = function() {
     var toprint = this.hasOwnProperty('myvar') ? this['myvar'] : 'defaultvar';
     console.log(toprint)
}


var myScope1 = {
     'myvar'  : 'var of scope1'
}


var myScope2 = {
     'myvar'  : 'var of scope2'
}

var myScope3 ={ //doesn't have a myvar
}

MyService.apply( myScope1, [] );
MyService.apply( myScope2, [] );
MyService.apply( myScope3, [] );


=== end code ======

copy the code above and paste them to google chrome's or firefox's console.