Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Initializing an Associative Array in Javascript

Ian presented the following question to me: How do you initialize an associative array with default values in javascript? There is plenty of posts out there on the internet in regards to creating arrays, most look something like:

var myArray = new Array();
myArray["something"] = "some value";


and so on. But what he was looking for was something more like the php method:

$myArray = ( "something"=> "some value", "something else" => "another value");

after a small amount of scouring remembering something about colons, I finally found a scathing post by someone with the revelation: Javascript doesn't actually have associative arrays. In fact javascript doesn't have arrays at all. It has objects. So how do you initialize an associative array in javascript? You don't. You initialize an object:

var arrayTest = {"something":"1st Result","second":"2nd Result"};
alert(arrayTest["second"]);


Hopefully that helps someone out there in Google land.

Javascript Variable Variables

Looking for a way to dynamically generate and assign variable names to objects in javascript, I found this post on creating a sort of variable variable in Javascript. Reading the post itself, I found the answer to my immediate question. However upon reading the comments, I feel like I had a moment of clarity in my understanding of the truly awesome power of javascript, dot notation, and prototype. Why is javascript so underestimated? I see this changing in the very near future...

http://www.i-marco.nl/weblog/archive/2007/06/14/variable_variables_in_javascri

Prototype vs OOP

Wikipedia tells me many things. Most importantly at the moment, it tells me how to move my fresh new conceptual understanding of object oriented programming over to Javascript, which is not Technically object oriented, by prototype based.

Yep, the same prototype I discovered and mentioned earlier.

Are you following along children? This rabbit hole keeps going deeper...

http://en.wikipedia.org/wiki/Prototype-based_programming


UPDATE:

This article is an excellent stepping stone for understanding prototyping in javascript. Excellent even for those with shaky knowledge of OOP.
http://www.packtpub.com/article/using-prototype-property-in-javascript

CallFunction() ... What are you?

Continuing working through SWFUpload code, I've come across

returnString = movieElement.CallFunction('' + __flash__argumentsToXML(argumentArray, 0) + '');

Now, I think I get the gist of what is happening here - CallFunction is an Active-X method which seems to take an XML style argument in order to call a function in the Actionscript code. However, googling around I seem to be completely unable to find an API or any definition of this method. Can anyone help me out with this?

Javascript IS object oriented...sorta.

http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/

Helps if you understand object oriented programming - of course, this article is so straightforward it can almost help you understand it!

The extremely long url says it all.

Terms: Static Members

Static Members are used with object oriented programming. A static member is a variable shared between all instances of an object. For example, if you had an object myObject with the static member objectCount, then myObject.objectCount++ would increase this value by 1 across all instances of myObject.

As javascript is not a typical object oriented language, the way object are created is slightly different. It seems typically they are assigned to a function - ie:

myObject = function(){ createObject(); }

and then

myObject.objectCount = 0;

Yes. This does mean you are assigning values to functions. I know! I'm still trying to wrap my head around it as well. I'm not even sure I'm using the right terminology to define this. So HA!

Let's just worry about understanding the definition of static member for now, shall we?

Tems: Prototype

Hello again children. Today we are going to learn about a nifty little property in Javascript - PROTOTYPE.

First off, have you ever worked with objects in Javascript? Tell me about it in the comments if you have. There is some debate over whether Javascript is truly an object oriented programming language or not (see here: http://javascript.crockford.com/javascript.html to learn why we tend to underestimate this language.) Either way, the prototype property is meant for working with javascript objects. Since I don't really know much about Object Oriented programming myself (which I plan to soon change!), I can't honestly give you a full run down on what it means/does. I'm just going to tell you the gist and give you the link so you can read for yourself.

Basically the idea is by using someObject.prototype.someMethodOrValue = something, you are in fact assigning that method or value to all instances of this object.

I have a hunch that this little feature may be at the heart of what makes JS object oriented. But I think I'll have to get back to you on that once I actually KNOW something about it. In the meantime, read about it for yourself.

Ta ta for now!

Walking the plank, er, DOM

A very handy reference for DOM objects and methods:
http://www.howtocreate.co.uk/tutorials/javascript/domstructure

Learning Ajax, Json, with php and database manipulation.

I've been struggling today and last week to find some good tutorials or a well recommended book for getting the ball rolling in my quest to learn Ajax. I figure I might as well try to start off working with Json instead of wasting time with parsing string data. So far here are the links I've found useful:

Quick run down on JSON:
Understanding JSON: the 3 minute lesson
JSON on Wikipedia

Excellent Starter tutorial using Ajax (and ASP, but can be easily modified to a php date() function)
W3schools Ajax Example

More Advanced and In Depth Series on Ajax covering numerous topics, which I breezed through the first 3. A much longer read, but contains some really useful details:
Mastering Ajax: 11 Part Series from IBM developerWorks

After you do the W3 Schools Tutorial, I suggest moving on to this one, which uses Ajax, Json, Php, and Database manipulation. Heads up - I had to remove the $$link variable variables from the database functions in order for it to work.
Json Ajax Webchat Tutorial

Good luck, and if you know of some additional resources I would appreciate the links.

PHP in JS?

Seems some clever people are porting php functions into a javascript library. At the very least the md5 function should come in handy. Not sure what the security implications of that are however. Check it out for yourself:

http://phpjs.org/functions/index