Archive for April, 2010

Solution to my last post.

Posted in open source on April 26, 2010 by scottdowne

In my last post I was trying to create a function that receives a multidimensional array inside parameters like (2, 4, 5) but with an infinite amount that could be passed in, and I was having problems with pass by reference, instead of pass by value.

I fixed it, and came up with a very simple solution:


p.ArrayList = function() {
var createArrayList = function(args){
var array = new Array();
for (var i = 0; i < args[0]; i++){
array[i] = (args.length !== 1 ? createArrayList(args.slice(1)) : 0 );
}

return array;
};
return createArrayList(Array.prototype.slice.call(arguments));
};

I came across the problem of arguments only acting like an array, but not having access to many array like functions, like slice(). Solution.


Array.prototype.slice.call(arguments)

Array.prototype.slice gets the slice method from the Array object.
call() allows you to call an object’s function from the context of another object.
and the slice method would be called, using the function definition attached to Array, like it was part of arguments, and just return a copy of the array’s values, as an array. :D

copy a multidimensional array in js.

Posted in open source on April 25, 2010 by scottdowne

I’m working on ticket #587, for processing.js.

What I have to do is convert a custom ArrayList function to create a multidimensional array based on parameters passed in. For example:


var array = new ArrayList(4, 5, 6);

would create a 3D array, similar to:


int[][][] array = new int[4][5][6]

This is what I’ve put together:


p.ArrayList = function() {
var array = [],
tempArray = [];

for (var i = 0, aLength = arguments[arguments.length-1]; i < aLength; i++){
array[i] = 0;
}

for (var i = arguments.length-2; i >= 0; i--){
tempArray = array.slice();
array = [];
for (var j = 0, aLength = arguments[i]; j < aLength; j++){
array[j] = tempArray.slice();
}
}
return array;
};

It starts at the back of the arguments, and copies the array recursively until it’s done arguments[0], then returns the array.

In JavaScript arrays are copied by reference, and not by value, but if you use array.slice() it creates a copy of the array’s values, but not a copy of the array inside the array, those are still passed by reference. In other words, it only works for single, and 2D arrays, anything larger and you start getting arrays containing references to the same data, which is not good.

I’m going to have to find a better way to do this, it must be recursive, and must be based on the number of arguments.

OSD600 is over

Posted in open source on April 22, 2010 by scottdowne

After tomorrow, I’m done this semester, and the real test begins: will I continue blogging between OSD600 and OSD700. It’s already been a week since my last post, but not because of being almost finished, but because of English assignments and exams, and right now I have a breather (I never study hard the night before the exam, I just make sure I didn’t miss anything). I would like to say I’ll continue blogging for as long as the internet lives, but time is a better test. I really do feel like I’ve found a great spot in open source, and I’ll enjoy it while it lasts.

Anyway, I’ve been trying to do at least one push to processing.js a day, while I study, big or small, at least one contribution.

Over the last week though, I’ve really got into irc, and branched into a few of the other channels, which have a totally different atmosphere than the processing.js channel. I got involved in the open source vs proprietary argument, which despite being educational and funny, really is never solved. I’ve tried to get some to at least try it before they slag it, but I guess for some programming is for money *shrugs*. I’ve told them to at least take the osd600 class in the fall, because they are quite promising students, just bias and not willing to try certain things. I tried explaining that open source is a good way to get work experience to get those jobs that make all the money they keep talking about. For them to take OSD600, they wouldn’t be programming for free, but for marks, and that’s what they do in all their other classes.

JavaScript this keyword

Posted in open source on April 14, 2010 by scottdowne

I know this this keyword’s (in JavaScript) meaning changes depending on how it’s called, considering this class:


function Obj() {
this.item = "contents";
}

and using it like this:


var obj = new Obj();
obj.item += " of this.item"; // item now contains "contents of this.item"

the this. keyword means the object only when it’s called with new keyword, which declares it as an object.

Now, if I am to change the object slightly, like so:


function Obj() {
this.item = "contents";
var funct = this.funct = function(){
return this.item;
};
}

I have two functions that should return the item inside the object. var funct should be usable inside the object, and this.funct should be usable outside the object, and do the same thing. Cept, when a function is created with var funct, the meaning of this. changes again. I’m not sure what it changes into, but in the above example, when I call funct() from inside the class, it returns “undefined”. Everything makes sense, I’m just not sure what happens to this. in that crazy example. There is a way to fix this. You need to put the function inside of a closure, sending this, and using it as another word. For example:


function Obj() {
this.item = "contents";
var funct = this.funct = (function(obj) { return function(){
return this.item;
};})(this);
}

What this does is, instead of sending a function using the this. keyword inside, it creates an anonymous function, sending the object this, RENAMING it to obj, and returning a new function where this is changed to obj, hiding the double meaning of the this keyword, making it refer to the same thing no matter when and how it’s called.

I’ve been learning the meaning of this, and I thought I had it figured out. This just shows you can never know you know everything, only that you don’t know everything, by learning something you didn’t know :P

with(this) { … } // no more

Posted in open source on April 12, 2010 by scottdowne

Here is the source, and an example for processing.js with “with(this) { … }” removed.

I was having issues using the new getters and setters. What was happening was considering this JavaScrpt class.


function Foo() {
this.value = 0;
__defineGetter__('value', function() { return this.value; });
__defineSetter__('value', function(x) { this.value = x; });

this.getValue = function(){ return value; };
this.setValue = function(x) { value = x; };
}

The this. keywords scope wasn’t as intended. It was actually referring to the getter or setters, and not the object that uses it. I solved this by putting it into a closure, and passing the object I wanted the getters and setters to be added to, like so:


function Foo() {
this.value = 0;
__defineGetter__('value', (function(obj) { return function() { return obj.value; };})(this));
__defineSetter__('value', (function(obj) { return function(x) { obj.value = x; };})(this));

this.getValue = function(){ return value; };
this.setValue = function(x) { value = x; };
}

OSD600 release 0.3

Posted in open source on April 12, 2010 by scottdowne

I guess technically I’m late on this release, but these things happen when you’re doing stuff with this ;)

I have been back and forth on this private ticket for while, trying different things, doing research, and then finally worked with Dave to get it perfect. I did get lots done on it, even thought it’s not all going to be used, I consider the research and work I did to be part of my 0.3. Here is a post on what me and Dave worked on, and this is a better foundation for future work in the class parser.

I also worked with Andor Salga. One of his 3D examples had a silly parser error, so I filed a ticket and fixed it. It was up for review, but I had to back up because it was relying on code from a previous fix that’s not going in. It was a simple parser fix, so it won’t be hard for me to re implement it when the time comes. Also, Andor has a way around it, so his 3D example does work now. This is the example he was working with, if anyone is interested.

I was able to fix, and not have to back up ticket #536 and it’s awaiting review.

I have learned one very important lesson; if two, or more bugs reside in the same spot in the code, I should consider keeping them in the same ticket, then I wouldn’t of had the problems I’m having with ticket #133, mainly, having to revert a bunch of my “peer review requested”. Really though, with the getters and setter, these are all going to be easy to refix (also, because I’ve already fixed them).

processing.js private and public finally working, I think

Posted in open source on April 10, 2010 by scottdowne

I think I have finally finished processing.js ticket #133.

A simple test to see it.

And a cool example that uses public variables. This particular test program was giving me the most difficulties while I was coding. It seemed whenever I would run the automated tests, this one would always have a new problem, and it also happens to be a long test, so it’s harder to debug, so to be able to edit it and change the example is satisfying.

I just need to write some clear, simple, and thorough test cases for private and public use, and I’ll be done.

Follow

Get every new post delivered to your Inbox.