When is everything alright with a JavaScript namespace alias, type or method?

Our team references the JavaScript JavaScript Style Guide .

One section says, β€œAvoid accessing alias properties if it's not an enumeration,” but it doesn't specify why.

Merging the method changes the context of "this" according to the sample code that we executed based on this article .

However, I cannot find a clear example of why you should not access the properties of aliases? Does anyone have an example of why this would be bad?

+5
source share
1 answer

There is no such thing as a JavaScript namespace , method or type . Well, maybe there are things like methods (let me just call them functions), but namespaces and types can mean different things to different people. For Google, they mean everything you find in your style guide.

JavaScript is a prototype language devoid of some of the features found in C ++ or Java (polymorphism, inheritance, namespaces, typing, Eve objects, etc.). The Google Style Guide (not one of a kind, see Douglas Crockford) presents a number of stylistic limitations. These restrictions often have no syntactic meaning, but are simply stylistic in nature (as my comment mentions).

For example, all namespaces must be aliases through goog.scope . This can be used for compiler closure compatibility, readability, debugging, or simply because it looks beautiful. To solve your question, it would be nice to have access to properties like aliased, and even the problem you are causing (to lose this context) can be solved with .apply(...) or .bind(...) .

In addition (and this is only my disclosure), the term aliased type has a rather specific meaning in other languages; I think it's foolish to use it in a JS context - which has an incredible vague idea of ​​types to start with.

+2
source

Source: https://habr.com/ru/post/1200160/


All Articles