Hello All,

I'm just learning JS although I have a fundamental knowledge of Actionscript 3.0 (which is based on JS). I'm a little confused right now as I'm following the lessons on appendTo.com. It seems in JS functions are made into variable and those variable are invoked. Is that how it works? In AS 3.0 funcitons are given names then invoked later. It's kind of messing with my head since I'm used to As 3.0. If that's just how the language works I'll just have to get used to it.

Hello Reliable,

JS is much more dynamic then AS 3, I used to work with AS3 with Flex, but I didn't miss a thing when I got back with JS.

There's multiple ways to define an JS function, some of them are:

// only functions
function myFunc() {};

var myFunc = function() {};

//function inside objects
var myObj = {
    prop: '',
    method: function() {

    }
};
myObj.method2 = function() {

};
myObj.method();
myObj.method2();

//Function prototype
var MyFunc = function() {}
MyFunc.prototype.Method = function() {};

var obj = new MyFunc();
obj.Method();

Also, there's multiple ways you can call a function in JS:

var func = function(param1, param2){
    // do something
};

func('a', 'b');

func.call(func, 'a', 'b');

func.apply(func, ['a', 'b']);

// you can even create a function with no name and execute it at the moment (used to control variable scopes)
( function(param1){
    // do something with param1
} ('value of param1'); )


// functions can also be passed as parameter, much used for callbacks
var func2 = function(callback) {
    callback('result of something');
}

func2(function(result) {
    alert(result);
});

// or you can pass the reference of the function
var func3 = function(result) {
    alert(result);
};

func2(func3);

Hope you can understand something inside this mess I wrote =D

'm following the lessons on appendTo.com. It seems in JS functions are made into variable and those >ariable are invoked. Is that how it works?

Not really, but the guy at appendTo is most probably trying to impress beyond reason.
Feel free to write your functions the way you're used too and do it with confidence.

Things written like:

var myfunc = function(){...};

are called: Anonymous functions. (They may be useful, but are not a [language] requirement).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.