Tuesday, December 30, 2014

Finding the hostname for an IP address on Windows

When trying to investigate networking issues, most people know about ping and nslookup. I discovered today that there's also a utility called nbtstat to get some NetBIOS info, including the hostname and MAC address from an IP address.

nbtstat -a IPADDRESS

Friday, December 12, 2014

RequireJS and TypeScript

Here is an example of a RequireJS module:
1
define([], function() {
2
    function Calculator() {
3
    }
4
 
5
    Calculator.prototype.calculate = function (x, y) {
6
        return x + y;
7
    };
8
 
9
    return Calculator;
10
});
To make sure this is compiling through the TypeScript compiler, the first step is to convert it to use modules from TypeScript. The simplest way is:
1
function Calculator() {
2
}
3
 
4
Calculator.prototype.calculate = function (x, y) {
5
  return x + y;
6
};
7
 
8
export = Calculator;
To compile this, you then call the compiler with the right module flag, which is AMD for RequireJS:
1
tsc Calculator.ts --module amd
This will then generate the equivalent JavaScript. You'll notice it adds some extra code that's probably unneccesary, but hey, I'll take that for compile time safety:
1
define(["require", "exports"], function(require, exports) {
2
    function Calculator() {
3
    }
4
 
5
    Calculator.prototype.calculate = function (x, y) {
6
        return x + y;
7
    };
8
 
9
    return Calculator;
10
});
If you're working in Visual Studio, you might get an error about it not using the module flag. If you're working with an existing codebase, you might find you'll need to add TypeScript support first - adding a new TypeScript file in VS2013 does this by adding some stuff to the CSPROJ file, namely the XML element TypeScriptToolsVersion, and will need to reload the project. Afterwards, there should be a new tab for TypeScript Build options in the project properties dialog where you can choose the module style:
404 - Image Not Found

Monday, October 20, 2014

JavaScript - private variables

Here's an example of the Module Pattern, read the comments to understand how it works:
1
// module is the value of what is returned from the Immediately Invoked Function Expression (IIFE).
2
var module = (function() {
3
  // 'Private' variable, defined within the scope of the executed IIFE.
4
  var _version = "1.0.0.0";
5
  
6
  // Returns a new object, with the property 'getVersion' that is pointing to an anonymous function.
7
  return {
8
    getVersion: function() {
9
      // When the anonymous function was defined, it was executing in the context of the IIFE
10
      // and so has access to _version.
11
      return _version;
12
    }
13
  };
14
})();
15
 
16
module.getVersion(); // returns "1.0.0.0"
17
 
18
// If we try set _version, what we're doing is setting the property on the object,
19
// and not the variable defined in the IIFE - we're not executing in that context,
20
// so we don't have access to it.
21
module._version = "2.0.0.0";
22
 
23
module.getVersion(); // still returns "1.0.0.0"
So, this solves the problem of having a private variable that we don't want to expose. Now lets see an example of a "class" in JavaScript (note: future versions will have class constructs built into the language, I'm just showing how people do it today):
1
// By convention, if a function is a constructor function, use UpperCamelCase
2
function Person(firstName, lastName) {
3
  // constructor when used with the 'new' keyword
4
  this.firstName = firstName;
5
  this.lastName = lastName;
6
}
7
Person.prototype.getName = function() {
8
  return this.firstName + ' ' + this.lastName;
9
};
10
 
11
var p = new Person('joe', 'bloggs');
12
p.getName(); // returns 'joe bloggs'
13
p.firstName = 'fred';
14
p.getName(); // returns 'fred bloggs'
This works, but the property 'firstName', is being used by the getName function, and is public. This means we can just override the property.
How would we create a private property in this case? Well, what if we try the same concept of scoping things to the execution of the constructor function, first attempt:
1
function Person(firstName, lastName) {
2
  // these are private to the constructor function
3
  var _firstName = firstName;
4
  var _lastName = lastName;
5
}
6
Person.prototype.getName = function() {
7
  // Won't work, since 'this' is scoped to the object that getName is executing on
8
  // and not to the scope of the constructor function.
9
  return this._firstName + ' ' + this._lastName;
10
};
11
 
12
var p = new Person('joe', 'bloggs');
13
p.getName(); // returns 'undefined undefined'
The function now doesn't have access to the variables, so what if we move this into the scope of the constructor... second attempt:
1
function Person(firstName, lastName) {
2
  var _firstName = firstName;
3
  var _lastName = lastName;
4
  Person.prototype.getName = function() {
5
    return _firstName + ' ' + _lastName;
6
  };
7
}
8
 
9
var p = new Person('joe', 'bloggs');
10
p.getName(); // returns 'joe bloggs'
11
var q = new Person('fred', 'flintstone');
12
q.getName(); // returns 'fred flintstone'
13
p.getName(); // also returns 'fred flintstone', since all objects are referring to the same prototype
At first glance, this seems to work, but we actually override the prototype each time the constructor runs... bad! Instead we should rather be defining the function on the actual instance being created:
1
function Person(firstName, lastName) {
2
  var _firstName = firstName;
3
  var _lastName = lastName;
4
  this.getName = function() {
5
    return _firstName + ' ' + _lastName;
6
  };
7
}
8
 
9
var p = new Person('joe', 'bloggs');
10
p.getName(); // returns 'joe bloggs'
11
var q = new Person('fred', 'flintstone');
12
q.getName(); // returns 'fred flintstone'
13
p.getName(); // returns 'joe bloggs'
This then solves the problem of keeping the variables private to each instance. One downside to this then is that each instance is referring to its own function, and not sharing one common function on the prototype, taking up more memory, but giving the advantage of private scope.
For some extra reading on scopes in JavaScript, read this - http://javascript.crockford.com/private.html
Another similar way of doing this is with the Revealing Prototype Pattern, explained in Dan Wahlin's post here - http://weblogs.asp.net/dwahlin/techniques-strategies-and-patterns-for-structuring-javascript-code-revealing-prototype-pattern