Javascript objects, for a C# developer

Yan Cui

I help clients go faster for less using serverless technologies.

Coming from a C# background, the thing that has struck me the most in learning javascript is its combination of dynamic and loose type system and protoypal inheritance model (more on this later).

Objects in javascript are very different from those in C#, though the ExpandoObject type in C# 4 offers similar ability to add/remove properties dynamically, javascript’s type system offers even greater freedom! Here’s a list of quick bullet points which hopefully can help clear some of your confusions around javascript’s objects and type system.

Primitive types

  • There are three primitive types in javascript – Number, String and Boolean
  • Integers and floats are both of number type
var integer = 8;
var float = 8.3;
typeof(integer) == typeof(float); // true
  • Use the isNaN() global function to see if a value is ‘Not-a-Number’
isNaN(123); // false
isNaN(1.23); // false
isNaN(10-2); // false
isNaN("Hello World"); // true
isNaN("2010/01/04"); // true
  • Use the parseInt() function to explicitly parse a string to integer, which keeps the integer portion of the string
parseInt("8"); // 8
parseInt("8.6"); // 8
  • Use the parseFloat() function to explicitly parse a string to float
parseFloat("8.6"); // 8.6
  • String quote types are interchangeable, but must match
"Hello World" == 'Hello World'; // true
  • There are no char type in javascript
typeof("Hello World".charAt(1)); // string

Special cases

  • There are two special cases – Null and Undefined
var d;
typeof(d); // undefined

var array = ["one", "two", "three"];
array[0]; // one
array[3]; // undefined

Object types

  • Arrays are objects
var array = ["one", "two", "three"];
typeof(array); // object
typeof([]);
  • Array items don’t have to be the same type
var array = ["one", 2, false];
array[0]; // one
array[2]; // false
  • Use pop() and push() functions to add/remove items from an array like a stack (LIFO)
var array = ["one", "two","three"];
array.pop(); // array = ["one", "two"];
array.push("four"); // array = ["one", "two", "four"];
  • Functions are objects
  • An object in javascript is basically a set of name value pairs where the names are strings and the values are strings, numbers, booleans and objects (including arrays and functions)
  • You can create a new object by specifying the name value pairs
var person = { Name: "John", Age: 10 };
person.Name; // John
person.Age; // 10
  • You can dynamically add property to an object using literals
var person = {};
person.Name = "John";
person.Age = 10;
person.Name; // John
person.Age; // 10
  • You can dynamically add property to an object using strings
var person = {};
person["Name"] = "John";
person["Age"] = 10;
person.Name; // John
person.Age; // 10

Type coercion

  • Type coercion = the type conversion which happens implicitly
8 + "8"; // 88
8 + 8 + "8"; // 168, conversion happened when the string "8" was encountered
  • Type coercion also applies to comparisons
8 == "8"; // true
1 == true; // true
  • Prevent type coercion in comparisons by using the === (exactly equal) operator
8 === "8"; // false
1 === true; // false

So that’s it, a quick run through of some of the things you would want to know about types and objects in javascript having come from a language static typing system such as C# or Java.

References:

Object oriented basics of Javascript

Understanding loose typing in Javascript

Learning Programming Languages with Koans


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


1 thought on “Javascript objects, for a C# developer”

  1. Pingback: Javascript’s prototypal inheritance, for a C# developer | theburningmonk.com

Leave a Comment

Your email address will not be published. Required fields are marked *