4

I happened to notice Javascript also supports the Function keyword just as in ActionScript. Since both are derived from ECMA Script, existence of such similarities is a common knowledge. But I was curious on what Function represents in case of Javascript, if not a Class(as in as3).

In JS,

var func = new Function(); or var func = new Function;

Assigns a function called 'anonymous' to the variable 'func'.

Could it be simply a method to implement the actual 'function' in an Object oriented way..

Since most browsers(ff, ie & chrome) seem to implement it in the same way, is it addressed in the spec?

loxxy
  • 12,505
  • 2
  • 21
  • 52
  • Check this link [link]http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname – Core Feb 25 '13 at 14:27
  • 1
    `Function` isn't really a keyword. It's just an identifier used as the name of an object constructor. Functions are objects, but there's different syntax allowed for creating them. – the system Feb 25 '13 at 14:29
  • 3
    Here: http://es5.github.com/#x15.3.2 – Felix Kling Feb 25 '13 at 14:29
  • @FelixKling Exactly what I was looking for ... – loxxy Feb 25 '13 at 14:30

3 Answers3

6

Function is the "class" all function extend from. All functions are really Function objects.

(function(){}) instanceof Function === true

You can also use new Function to make a function from a string (like eval).

var func = new Function('x,y', 'return x+y;')
Rocket Hazmat
  • 204,503
  • 39
  • 283
  • 323
  • 1
    You forgot some quotation marks in your last example. They need to be separate string arguments to the constructor instead of a comma-separated string. – the system Feb 25 '13 at 14:31
  • @thesystem: They can be either/or :-) See the note at http://es5.github.com/#x15.3.2.1 – Rocket Hazmat Feb 25 '13 at 14:32
  • Really? My apologies. I never knew that. – the system Feb 25 '13 at 14:32
  • 1
    @thesystem: Nope. A comma-separated string is also allowed. See [MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function) and the note to http://es5.github.com/#x15.3.2.1. – Marcel Korpel Feb 25 '13 at 14:32
  • @RocketHazmat Well, whatever happened to the 'no classes' behavior of JS... thanks :) – loxxy Feb 25 '13 at 14:34
  • @loxxy: That's why I put `class` in quotes. It's not really a class, but I didn't want to confuse you :-) – Rocket Hazmat Feb 25 '13 at 14:54
2

"Every function in JavaScript is actually a Function object." MDN Documentation.

function myFunc(a, b) {
  console.log( 'test' );
}

is essentially equal to

var myFunc = new Function('a', 'b', 'console.log( "test" )');

There are however some differences between to two ways of declaring a function. Read the doc!

marekful
  • 13,318
  • 5
  • 30
  • 52
  • 1
    They're very similar, but there are some important differences. One is that the first syntax will be "hoisted". But more importantly, the second syntax will be evaluated in the global variable scope instead of the local one. JS interpreters don't use constructor functions internally. – the system Feb 25 '13 at 14:37
  • 1
    Thanks @the system, just clarified this by re-reading the relevant docs. Edited my answer. – marekful Feb 25 '13 at 14:43
-1

1 way:

var Person = function() {
    this.name;
    this.age;
}

var p1 = new Person();

2nd way:

function Person() {
    this.name;
    this.age;
}

var p1 = new Person();
Storm
  • 3,998
  • 10
  • 36
  • 54