Posts tagged with Javascript

Javascript Type Gotchas

May 1st, 2010
typeof null // -> object
null instanceof null // -> TypeError: Can't use instanceof on a non-object.
true instanceof Boolean // -> false
true == new Boolean(true) // -> true
true === new Boolean(true) // -> false

Needless to say, this is very annoying if you’re trying to be be careful with your types and just goes to show that not everything is an object in Javascript. Come back soon for my attempt at a solution.

You got pointers in my Javascript!

April 8th, 2010

Why yes, yes I did! Introducing pointy.js. Pointers, trees, circular linked lists, they’re all there. It’s been a decade since I used pointers in C++, so please bear with me if there are problems. Gentle corrections welcome!

Aggregation Strategies

March 3rd, 2010

How do you find the minimum, maximum, and average of values of a set of samples over time? What if some of your data sources are unreliable and prone to drop in and out? Should you group them into regular buckets, even if that may mean multiple samples from source A and none from source B? Or should you calculate instantaneous aggregate numbers whenever a new sample comes in at the expense of having as many aggregate points as samples and potentially unevenly distributed across time? Are there better ways?

In the end I hacked up a simple library, aggregate.js, to test various strategies using Node. Right now I just have the two basic windowing and instantaneous strategies implemented, but I plan on adding more as needed.

Javascript Constructors with an Array of Parameters

March 3rd, 2010

Say we have:

function myObj() {
  this.input = Array.prototype.slice.call(arguments);
}

We can pass a variable or an array but :

var myObj1 = new myObj(1);
myObj1.input // -> [1]
var myArr = [1, 2, 3];
var myObj2 = new myObj(myArr);
myObj2.input // -> [[1, 2, 3]]

How then can we get, while still using myArr:

myObj3.input // -> [1, 2, 3]

Here’s how, courtesy of this site:

var myObj3 = new myObj();
myObj.prototype.constructor.apply(myObj3, myArr);

Note that myObj3 needn’t be an instance of myObj, it just needs to be an existing value so that the global object isn’t used as the value of this.

This is pretty cool, though of course you can do it much easier in languages such as Scala:

val myObj4 = new myObj(myArr: _*)

Javascript Primitives

February 13th, 2010

From the Mozilla Javascript Glossary:

primitive, primitive value
A data that is not an object and does not have any methods. JavaScript has 5 primitive datatypes: string, number, boolean, null, undefined. With the exception of null and undefined, all primitives values have object equivalents which wrap around the primitive values, e.g. a String object wraps around a string primitive. All primitives are immutable.

Did you know that all Javascript primatives are immutable? I sure didn’t.

My latest Scala-inspired Javascript library: pf.js

February 4th, 2010

What is a function? While many use the words ‘method’ and ‘function’ interchangeably, more mathematically-inclined programmers make a distinction between functions and methods based on between you are guaranteed a return value. Scala is one programming language that makes a distinction between functions and methods (though it’s actually pretty easy to jump between the two). Great, just like plenty of other languages, nothing special here. Javascript functions are funny beasts. On one hand, they seem to be simple subroutines which can be passed arguments and can enclose variables – pretty standard methods in other words. However, regardless of whether you include return statements in a function, something is actually returned, undefined being returned if you hadn’t specified a return value. Hmm, so they’re kind of like mathematical methods except there’s no way to know without looking at a function’s source code whether the author failed to return a value for the input you gave it or actually did return something, the undefined object.

However, Scala also provides a middle ground with PartialFunctions. PartialFunctions are cool because they are only defined for specific inputs and you can check this. In addition, because they are defined for specific inputs, you can combine several PartialFunctions to create a new PartialFunction which is defined for the union of all the original PartialFunctions’ inputs. Wouldn’t it be cool if Javascript did that too? Well, now it can with pf.js. Enjoy.

A Subtle Javascript Mistake

January 29th, 2010

I was just pounding my head against what turned out to be a simple Javascript misconception that I hope I can save people from: in tests for the existance of a key, not a value, in an object or array. So the following work:

  var myObj = {1: "one", 2: "two"}
  1 in myObj // -> true
  2 in myObj // -> true
  var myArr = [1, 2]
  0 in myArr // -> true
  1 in myArr // -> true

But you might be surprised at the following results (I was):

  2 in myArr // -> false

That is because myArr has values 1 and 2 at keys 0 and 1, respectively. To test the existence of a value in an array, you need to use indexOf. For instance:

  myArr.indexOf(1) // -> 0
  myArr.indexOf(2) // -> 1
  myArr.indexOf("doesn't exist") // -> -1

As you can see, -1 indicates the value does not exist in the array.

Introduction to Case Classes

December 11th, 2009

I’ve been recently chatting with the creator of match-js about how his library and caseclass.js might work together and I ended up writting quite a bit about case classes. Enjoy.

Scala is a newish object-oriented/functional hybrid language that runs on the JVM. It actually takes a lot of concepts from Erlang, such as its actor library. One of its key features is pattern matching, which at the most basic can be used to test equality. With the match operator that means you have something like switch/case in most languages:

val myVal = 3;
myVal match {
  case 1 => "one"
  case 2 => "two"
  case 3 => "three"
  case _ => "other"
} // returns "three"

Case classes at a glance are just simple classes that be created without the new operator and automatic getters and setters:

case class Person(name, age)
val peter = Person("Peter Robinett", 25)
peter.age == 25 // -> true

However, you can do a lot more advanced stuff:

unknownVar match {
  case n: String => println("unknowVar is a String with value: " + n)
  case n: Int if n > 0 => println("unknownVar is an Int with a value greater than 0")
  case Person(name, 100) => println("unknownVar is a Person case class where property age is 100 and name is: " + name)
}

The last case example shows a cool use of a case class. Scala knows that when a case class instance is created in a case, you are testing for matching. So, it calls the class’s unapply method (any class can have one, case classes just have them automatically) to compare the instance based upon its properties. If the parameters match, the two instances are equal. However, you can also not specify properties to match. If you don’t care about them all you can just give a wildcard (the _ character) or not give any parameters (case Person or case person: Person) but if you’d like to extract the value, you can. This is what I did. By giving it the undefined variable ‘name’, it knows that I am not comparing the name properties but instead want to assign unknownVar.name to name (assuming that unknownVar is an instance of Person!), which is then within the scope of the subsequent code block.

So, that’s a lot of Scala. What do I have in Javascript? Right now you can match based upon object types:

CaseClass.create("Person", ["name", "age"]);
var peter = CaseClass.Person("Peter Robinett", 25);
peter.match(
  {
    caseTest: "Car",
    caseFunction: function() { return 0; }
  },
  {
    caseTest: CaseClass.Person,
    caseFunction function() { return 1; }
  }
); // -> return 1 – I'm not a Car!

Parameter definition and extraction also works, though it’s limited:

var undef;
peter.match(
  {
    caseTest: CaseClass.Person("Barack Obama", 47),
    caseFunction: function() { return 0; }
  },
  {
    caseTest: CaseClass.Person("Peter Robinett", undef),
    caseFunction function() { return age; }
  }
); // -> return 25

As you can see, matching works fine though I need pass an uninitialized variable to Person() to indicate that I want to extract the corresponding property in peter. Because I cannot discover the name of this variable, I just have to create variables based upon the property names. I’d like to improve this, but the consensus seems to be that it’s impossible with Javascript today (ironically, earlier versions could). Beyond extractors, I’d like to be able to do more complex matching (better support for different object types, conditional stations) and be able to extend native objects to support my matching. I also like that your Match() method can stand alone, letting it be used as a callback and meaning that it acts as a sort of partial function, which is very cool. In Scala partial functions are functions that needn’t always return a value:

def myPF(num: Int) = {
  case 1 => "one"
  case -1 => "negative one"
}

To be honest, I’m not sure if our libraries could or should work together but they’re similar enough in spirit that I thought it might be exploring how they can complement each other. I see you’re doing stuff with web workers. I’ve also tried to take Scala’s actors and use them to do something in Javascript. actor.js is the result and is a place where I think case classes would be great for message passing (this is a key use case for case classes in Scala). However, actors are both more complex and something that I have less experience with, so I haven’t put as much time into it as caseclass.js. However, the demo does work and is, in my opinion, pretty cool!

Javascript Console

September 3rd, 2009

With all my recent Javascript experiments, I wanted a browser-independent Javascript console more and more. I found Rhino and have been using it for the last month or so. It’s proven pretty useful, and it was quite easy to install: I simply copied js.jar to /usr/local/rhino/ and then added the following to my .profile for convenience: alias js="java -jar /usr/local/rhino/js.jar"

I just learned about jsc, a console Apple includes with the JavascriptCore library that I believe every version of OS X has installed by default. It’s found at /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc, so again you’re going to benefit from an alias or adding the directory to your PATH.

While Rhino has more features, namely the ability to load Java libraries, the jsc experience is better: the console strikes me as more responsive (it definitely launches much faster), a command history is available and you can use ctrl+a and ctrl+e to jump to the beginnings and ends of lines. Whoever made jsc seems to have a functional bent, as every expression’s return value is printed out. This means you get undefined on screen a lot if you’re doing lots of loops and/or prints, which can be annoying.

Update, 2010-06-22: Using rlwrap (e.g. alias js="rlwrap java -jar /usr/local/rhino/js.jar") solves many of the problems I had with Rhino.

Speaking of that, I think they handle the return values of loops incorrectly. As I understand it, all loops should return undefined, i.e. nothing. But consider this jsc session:

> var a = 1; while (a < 4) { print(a); a++; }
1
2
3
3

3 is printed out twice because it is ‘returned’ by the while loop. Of course, while does not really return a value, as this session shows:

> var a = 0; var b = while (a < 4) { a++; }
Exception: SyntaxError: Parse error

But where does the 3 come from, the a variable or the loop’s condition? From the following sessions, I believe it comes from the last time the condition evaluates to true:

> while (1 != 1) {}
undefined
> var a = 1; while (1 != 1) { a; }
undefined
var a = 1; while (1 != 1) { a = 2; }
undefined
> var a = 1; while (a != 2) { a = 2; }
2

Note that when the condition never is true, there is no defined value outputted. Likewise, assignment within the loop has no effect on what is ‘returned’. Or rather, I wish it was that way. Consider this:

> var a = 0; while (a < 2) { a = 2; }
2
> var a = 0; while (a < 2) { a++; }
1

To be honest, I really am confused about what jsc is doing here. It seems wrong, though irrelevant since these values printed out cannot be assigned to a variable and used. Perhaps someone from the jsc team can explain this?

actor.js

August 29th, 2009

I just pushed actor.js to GitHub. It’s an easy-to-use wrapper of Firefox 3.5′s Web Workers, giving you a nice way to pass messages between threads via Actor objects which can listen to other Actor objects. It’s inspired by Scala’s Actor library, though beyond the name there’s probably nothing much in common – mine is surely much worse. Patches welcome!