Interfaces in AngularJS - javascript

So, I'm a big fan of using interfaces in Java. I am trying to relate that to AngularJS. I read up on the concept of duck typing for Javascript, which is fairly straightforward. Is there anything the base AngularJS library provides in addition to that that would be similar to the concept of an interface? I know javascript is loosely typed, but without using typescript, is there anything similar?
I'm only familiar with AngularJS to the point that I can jump into existing code and change functionality, I am not able to develop a well designed Angular application from scratch. We are using Angular version 1.X.

There's nothing angulary that would be similar to an Interface. You don't typically define an Interface in JavaScript, so most likely what you want to do is an anti-pattern in JavaScript.
Here's another SO question that digs into why duck typing isn't typically something you do with JavaScript.
Duck Typing in Javascript

I'm sorry but TypeScript is what you should use. That's my opinion. AngularJS is more like a framework that helps you with Model View Controller. It is not a library and it doesn't extend JS in that way.

the best thing is using Typescript for interfaces. Because JavaScript doesn't have interfaces. Here Typescript will transpiles your code to resulting JavaScript. I think Interfaces are only for strong typing and better tooling support to build a code without any errors.

Related

React is considered as a library and Angular is considered as a framework. Why so?

Please someone answer by justifying the definition of library and framework.
"Your code calls library whereas framework calls your code."
React cannot be considered a library given provided, absolutely correct, and only one that makes any sense definition of the difference between framework and library.
The library is just a set of methods/functions.
The framework provides a frame for work, it requires the code to be written in a certain way.
Considering the above, React obviously is the framework. Very complex, very opinionated framework.
Why it has been promoted as a library?
IMHO because promoters of React are cheap manipulators who wanted to steal the popularity of jQuery - the most popular JavaScript library.
Good library vs framework article. The author uses jQuery as an example of a library and Vue as an example of a framework. Probably, just in order to not tease geese. But Vue code from provided example is almost similar to analogic React code.
The reason is basically related to use case of both the technologies, as React is called as a library because of its behavior of easy to fit into any js environment and doesn't bound user to use things React way, while one the other hand Angular is complete MVC (Model View Controller) architecture, and restrict user to follow its pattern of framework.

Can I develop an Angular 5 app using JavaScript?

I'm new in web dev, and I am currently reading about Angular, and other frameworks. I know that Angular is written in TypeScript, but I'm wondering if it is possible to develop an app using Angular and JavaScript?
As TypeScript is a superset of JavaScript, existing JavaScript programs are also valid TypeScript programs.
(Wikipedia)
Meaning, All JavaScript is valid TypeScript. TypeScript only adds thing to the JavaScript language, you are not required to use these new features such as Types, you can skip them and just type normal JavaScript.
However, when you develop Angular applications you become a user of the #angular-cli package and tool which helps you generate boilerplate code for your files and projects. This code that is generated by the CLI is written using TypeScript features -- So if you for some reason refuse to use TypeScript, it will make your development experience worse since you will not be able to use all of the Angular tools that help your development, such as the CLI or the Documentation.
Yes.
Angular does use .ts files by default. But if you write simple javascript code in them it will still work. Valid javascript is also valid typescript.
Once you get up and running with angular though you will see the benefits of Typescript are worth it. It won't make sense to write an Angular app using just javascript.
The question you should ask yourself is why would you do that? TypeScript became a standard for large front-end applications. It stays ahead of the curve of EcmaScript, bringing more features that are only going to become part of the standard and then implemented. Not to mention static types, which ensure all sorts of runtime errors just never happen.
Also, TypeScript is a superset of JavaScript, you can write your ol' regular JS in .ts files and never worry about all the goodness TypeScript gives you if that is what you want.
Yes, You can but it would not be sophisticated and well-timed application If you want to avoid Typescript. Even ECMAScript 2017 also very similar to Typescript. So, It would be definitely better to go ahead with Typescript.
Introduction to Angular concepts
Angular is a platform and framework for building single-page client applications using HTML and TypeScript. Angular is written in TypeScript. It implements core and optional functionality as a set of TypeScript libraries that you import into your apps
read more

Angular2 application half in Typescript and Javascript

I currently have a large JavaScript application with a lot of code written in vanilla Javascript for a specific platform for where I work.
I need to transform this into a web application I can open in the browser. This should be able to be done relatively easy by swapping out the components from my current application to the components of another web technology.
However since Angular2 is written in TypeScript, if I was to choose it as the framework I use, should I really also be rewriting the rest of my non-component JavaScript to TypeScript to or will I not need to do this.
Would it be a bad idea to have an application which was half TypeScript and half JavaScript like this?
It's not a bad idea to mix TypeScript and JavaScript. I do it every day. But you have to now that it may be very painful in some cases. So I can only advice to enforce migration to TypeScript as fast as possible.
I would suggest you to read this introduction to migration from JavaScript to TypeScript: https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html
This might also be a valuable source: https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html but I have the impression that the author has a very narrow opinion on how to work with typescript so just don't take everything there as it is, but just as a suggestion.
There are also a lot of other resources about migrating from JavaScript to TypeScript.
For the last month of migration to TypeScript I can say, that in many cases you can simply change your JavaScript to a TypeScript file and add exports. Than you need to add the needed imports in the other places and everything works just like before.
If you can make sure, that you only use old JavaScript in your TypeScript and not the other way round you can split your project into to packages. Load the JavaScript into global as done before and than create Typings for the parts of your JavaScript you use in TypeScript.

Angular 2: Mix Typescript, Javascript and Dart

Is it possible to mix the three Languages in one Angular-2 Project, e.g. writing the App in Typescript a Componet in Dart an another in JS?
JS is TypeScript. TypeScript is a superset of JavaScript but if you use the Angular2 JS library to build components, it's unlikely that these components will work with components built with TS (not entirely sure here).
You definitely can't mix Dart with any of the other two.
An Dart Angular2 application depends on being built as a whole and this won't work with mixed in JS or TS components.
What can be done is to bootstrap 3 root components where each one is built with a different language, but within one root component mixing languages is unlikely or impossible to work (see above).
Yes, it is possible since they are all will be translated to JS, but I would not recommend doing that.
The reason to avoid such a mix is that it will be difficult to maintain the code. Each of these languages has its own patterns and ways of doing things. By mixing them you will need to create redundant abstractions and mappings (e.g. TypeScript typings for Dart's components), unit-tests will be segregated by technology. Just imagine that one day you will need to add one more developer to the project. If there is only one of these technologies the search and evaluation of a candidate will be as simple as specifying a single skill in a job description. But when you have 3 of them it becomes difficult to find a developer having all these skills. Even if you will find this "ninja" there is still a need to teach him how you are using the mix. You will need to explain to him all hacks/tricks of build scripts and then transfer a sacral knowledge when and why to prefer one technology over another.
I would recommend you to stick to TypeScript because it is a superset of JS and its features are compliant with ECMA standard.

Is there an CDI-like mechanism thinkable for JavaScript

Before diving into JavaScript I was doing a lot of Java and especially liked the concept of context dependency injection. I used Google Guice a lot.
I know that JavaScript is not statically typed. And there are modulization concepts like used by requireJs or Node.js (even did something myself).
But is it thinkable or are there already solutions to inject dependencies based on a loosely defined interface.. (hope that isn't too stupid to ask)
The closest thing I have seen to what you are asking for (I think) is Architect from tim caswell.
It allows you to register modules abilities and requirements, so you can essentially request something that can preform an action, and have Architect check all the available modules to see if any of them support such an action.
You may also want to take a look at Errai (http://www.jboss.org/errai). It brings the client and sever together using the same programming paradigm by using the GWT javascript compiler bringing CDI to the browser (they're also working on JPA in the browser as well).

Categories

Resources