piątek, 26 lutego 2016

Angular 2 Component - select property... ohhh I mean selector :-)

I wondering is there a way to mark some properties of Angular2 component as required as for example selector property?

I assume that the selector property is specific for browsers, and maybe on another platform this property is not required, but as far I use bootstrap to launch my app, Angular2 know that I will lunch this in browser, so maybe a small error like, "component [xxx] dont have selector property", would be a nice thing :-)

I just spent almost one hour wondering why is my code not working, where I did very simple example based on tutorial from Angular2 website to just show one component in another component.
Below I put example of my invalid component :-) of course this will not work, but I dont get any error about this situation in console. Of course as always it was my mistake. I made a typo :-(.

I really like Angular2 but there are so many things to learn in this framework, that this kind of property checking as some additional option to switch on would be great and help a lot.

Just my thoughts, or maybe I just need to buy glasses :-)

środa, 17 lutego 2016

ES6: Const or let or var?

ES6 / Es2015 introduces a lot of new extensions to the JavaScript world. One of these extensions are two new keywords used to variable declarations. So now in addition to the old var keyworld we have got let and const that allows us to declare a variable with a block scope. Oh great it may seem, we have so much choice, but the choice becomes problematic when it comes to writing code …

Suddenly you start writing code, you need to declare a variable and … there are questions like: when I use const, and when let, can I still use the var? These types of questions began to appear also in my team when we started to write in Node.js with ES6. It was important for us because we want in a team cohesive approach to the declaration, and because each member had a little different view of this, we had to discuss it …

First, we got acquainted closer with the let and const. about which many people have already written nice articles. I pasted below some interesting links to this topics.

David Walsh Blog. For and against let Examining JavaScript ES6’s new ‘let’ keyword block for scoping, and conditions where it should or should not be used.
Tearing down the confusions around the ES6 ‘const’ keyword
Pony Foo. Let ES6, Const and the “Temporal Dead Zone” (TDZ) in Depth This is yet another edition of ES6 in Depth. First time here? Welcome! So far in the covered destructuring, template literals,> arrow functions, the spread operator and rest …
and the last https://hacks.mozilla.org/2015/07/es6-in-depth-let-and-const/

Discussions on how to declare variable led my team to a discussion at the workshop within the company. Effect of such a summary here…

Steps to follow when declaring variable:

  1. If the variable is used throughout the function, use const or if a variable need reference rassigned , use var
  2. If you need a variable with block sopce (for example, in if condition), use const, if variable need to be assigned to a another object, use let
  3. use let with for (;;) loops.
As you can see we we initially start with const, becouse thanks to this we secure ourself from accidently reasigned veriable.You need to remember that With const you only receive unchenged reference to object but you can change objects properties. Objects declared with const are not immutable unless you use Object.freeze() (of course with nested object you shoul use Object.frezee() with recursion).

Quick summary

Is it realy worth it to follow some trends, fashion, not understanding why you should use this approach over another, or maybe it just because it looks prettier in the code? Why I should not use var if I know that my variable which I declared on top of the function will be used all over the function body…
My small advice, learn/read about new stuff to get better understanding and do whats best fit to you and your team and stick to those choices…