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?

3
  • Check this link [link]stackoverflow.com/questions/336859/… Commented Feb 25, 2013 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. Commented Feb 25, 2013 at 14:29
  • 3
    Here: es5.github.com/#x15.3.2 Commented Feb 25, 2013 at 14:29

3 Answers 3

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;')
Sign up to request clarification or add additional context in comments.

6 Comments

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.
@thesystem: They can be either/or :-) See the note at es5.github.com/#x15.3.2.1
Really? My apologies. I never knew that.
@thesystem: Nope. A comma-separated string is also allowed. See MDN and the note to es5.github.com/#x15.3.2.1.
@RocketHazmat Well, whatever happened to the 'no classes' behavior of JS... thanks :)
|
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!

2 Comments

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.
Thanks @the system, just clarified this by re-reading the relevant docs. Edited my answer.
-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();

2 Comments

This is about function expressions and function declarations, not about the Function constructor.
This is not what the question is asking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.