未加星标

10 Most Shocking JavaScript Features for C# Developers

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏
Whether you’re a C# (or similar language like Java) developer eager to learn javascript or you’ve already been working with JS for some time, I hope you find this article interesting. I collected for you 10JavaScript features which are/were the most shocking for C# developers who learnt JavaScript.These features of JavaScript language are the most striking differences compared to C# ecosystem. If you’re about to learn JavaScript, sooner or later you’ll also have to deal with them. Before we begin, one disclaimer: the goal of this post isn’t stating which language is better or worse. JavaScript and C# are different programming languages and their use cases are also completely different. One is better for some usages, while the other is better for others. The list is my and other readers’ subjective one and you don’t need to agree with all points. I’m myself a C# developer on my JavaScript learning journey, so I’d like to help you grasping these confusing concepts :wink: 1. Dynamic Typing Obviously, the first difference JavaScript newbie notices is that the language is dynamically-typed . It means that the types (of variables, functions, actually of almost everything) are checked at runtime , not at compile time like in C# or Java.Because of that, there’s not much difference in defining variables and assigning them data of various data types :JavaScript’s variables are not associated with any particular data type. That’s why it’s completely legit to write something like that:Because JavaScript is dynamically-typed, issues with types are detected at runtime (e.g. error is thrown as soon as you try to use a variable in a context which expects another data type), not during compilation time as it would be in C# or Java.

If you have some experience with statically-typed languages, you may now feel how much confusion and troubles dynamic typing can bring. At least at the beginning.

2. Implicit types coercion

JavaScript has one more interesting feature types coercion . Even though this term’s name sounds a bit weird , it’s quite simple: types coercion happens when a value of one type is converted to another one .

It may be explicit, but harder to grasp is the implicit types coercion . It means that developer doesn’t necessarily ask for converting value of some type to another, but it still happens “silently”. To visualize it, consider the following code:

Even though console.log() expects data of text type, isGreatDeveloper which holds a boolean value at the moment is implicitly converted to text.

The above example was just to show you what types coercion is all about, but it gets more confusing in reality. First misleading concept is that JS has actually two equality operators: == and === .

Let’s see some code example to know the difference between them:

The first == operator performs an implicit types coercion before performing the actual comparison . That’s why the text ’26’ is “silently” converted to a number 26 and the if statement is true at this point.

Because of that, we should normally always use === operator to avoid such unclear situations.

Existence of such (and many more) nice concepts of JavaScript leads to finding such interesting Tweets:

*shuts laptop forever* pic.twitter.com/3GS1JfNDSP

― Shayna Gentiluomo (@kd2luw) May 3, 2018

If you want to dig into types coercion, you can read this article .

3. null and undefined In JavaScript there are two values which represent kind of unassigned variables: undefined and null

. What’s the difference between them? The snippet below clarifies:

As soon as you declare a variable, but don’t assign any value to it, it contains undefined . It may contain null if it was assigned to it, either explicitly as in the example or as a result of some operation.

I see it as the names suggest: undefined is something that has really not been defined (assigned) yet, while null is more like a special kind of value which has some useful meaning .

4. Truly and Falsy values

As we already know null and undefined , it’s important to note that there are some “falsy” and “truly” values in JavaScript:

“falsy” values: undefined , null , 0, ” , NaN “truly” values: all NOT falsy values

It means that “falsy” values are implicitly converted to false boolean value e.g. in comparison context:

Knowing that you might want to check if the value has been defined in the following way:

However, assuming x is a number, this will not work if x has a value of 0 , which is also one of the “falsy” values.

So the proper way of checking whether the number has been defined is as follows:

Of course, for another data types (like text) you need to handle other “falsy” values as well. It depends what you consider a meaningful value.

5. Hoisting In JavaScript you can legally write such a code:As you can notice, name variable is used before it’s declared 2 lines below. This is maybe not that weird, as in JS we can even declare variables without the var keyword, but we can do the same with functions:So here we call the function before declaring it. Why does this work?This works thanks to hoisting . It’s a compile-time mechanism which “moves” all variables and functions declarations to the top of our JavaScript code . In fact, during the execution of above-listed code, JavaScript engine processes variables and functions declarations before executing any other code.

It however applies only to declarations, not to assignments:

In this case, only var name part of the declaration and assignment is “moved to the top” and executed first. The assignment part ( =5 ) is not. That’s why when calling console.log(name) the name variable is undefined .

Because of hoisting, we should declare and preferably assign all variables used in the given scope at the top of it.

6. Lexical scoping and this keyword To create a scope (space in which the variables defined in it are accessible) in JS, we need to create a new function. Variable defined in a function is not accessible outside this function:It means that other blocks like the ones associated with if statement don’t introduce a new scope:If a variable is defined outside of any function, it’s assigned to a global scope .However, in JavaScript we also have something called lexical scoping . It basically makes functions declared within another functions having access to the parent function’s scope , as the following example presents : Forming such “hierarchy”

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:10 Most Shocking JavaScript Features for C# Developers
本站链接:https://www.codesec.net/view/610971.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(52)