Skip to content

Instantly share code, notes, and snippets.

So I want to validate something exists on a JS object:

function doSomething(config) {
  // Throw if config.required is not present. 
}

The most obvious way I found to do this at first was to rely on JS's "truthiness" to determine it:

function doSomething(config) {

if (!config.required) {

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0, 1">
<LinearGradientBrush.GradientStops>
<GradientStop Color="White" Offset="0.0" />
<GradientStop Color="Black" Offset="0.4" />
<GradientStop Color="Black" Offset="0.5" />
class FooBase
{
public FooBase()
{
CreateBar();
}
// "Bar" is usually a pipeline of helper classes that process something Foo is interested in
// The pipeline varies in each derived type. Maybe "Initialize" is better.
protected abstract void CreateBar();
abstract class FooBase
{
public string GetMessage()
{
return GetMessageCore();
}
protected abstract string GetMessageCore();
}